combine assimp bone data with other vertex data - assimp

My problem is that I am trying to load in joint/bone data from an fbx file in Direct X c++ using Assimp, but I want to store the eights and indices inside the same vertex struct that I store position, uv, etc.
I can make a loop for every vertex, but I also want to make a loop over each bone.
That means I can't have both the joint data and the other data in the same loop.
Should I create multilpe vertec objects then combine them afterwards?
I also am not sure how to find the bone ID and the weight for each vertex, I am counting on 4 per bone, but maybe I should not have that last loop at all?
Im not sure how to set it up.
I would appreciate some help, thank you very much.
for (UINT k = 0; k < currentMesh->mNumBones; k++)
{
aiBone* bone = currentMesh->mBones[k];
for (UINT m = 0; m < bone->mNumWeights; m++)
{
aiVertexWeight weight = bone->mWeights[m];
for (UINT n = 0; n < 4; n++)
{
//if
}
}
}
//////////////////////////////////////////////
for (UINT k = 0; k < currentMesh->mNumVertices; k++)
{
Vertex vert;
vert.position.x = currentMesh->mVertices[k].x;
vert.position.y = currentMesh->mVertices[k].y;
vert.position.z = currentMesh->mVertices[k].z;
vert.TexCoord.x = currentMesh->mTextureCoords[0][k].x;
vert.TexCoord.y = currentMesh->mTextureCoords[0][k].y;
vert.normal.x = currentMesh->mNormals[k].x;
vert.normal.y = currentMesh->mNormals[k].y;
vert.normal.z = currentMesh->mNormals[k].z;
vertexVector.push_back(vert);
}

for (UINT k = 0; k < currentMesh->mNumBones; k++)
{
aiBone* bone = currentMesh->mBones[k];
for (UINT m = 0; m < bone->mNumWeights; m++)
{
aiVertexWeight weight = bone->mWeights[m];
if (vertexVector[weight.mVertexId].joints.x == 0)
{
vertexVector[weight.mVertexId].joints.x = k;
vertexVector[weight.mVertexId].weights.x = weight.mWeight;
}
else if (vertexVector[weight.mVertexId].joints.y == 0)
{
vertexVector[weight.mVertexId].joints.y = k;
vertexVector[weight.mVertexId].weights.y = weight.mWeight;
}
else if (vertexVector[weight.mVertexId].joints.z == 0)
{
vertexVector[weight.mVertexId].joints.z = k;
vertexVector[weight.mVertexId].weights.z = weight.mWeight;
}
else if (vertexVector[weight.mVertexId].joints.w == 0)
{
vertexVector[weight.mVertexId].joints.w = k;
vertexVector[weight.mVertexId].weights.w = weight.mWeight;
}
}
}
I accessed the same structure I already have and added the stuff afterwards.

Related

How to access the values of a dictionary property in a grid using Ocean for Petrel?

I'm tring to access the values of a dictionary property in a grid,such as Fluvial facies or lithologies etc.I have read the coursebook and help docs, but didn't find anything relevant.The coursebook only has examples of creating properties, but not accessing properties.Below is the code I tried:
Grid grid = arguments.Input_Grid;
if (grid == null)
{
PetrelLogger.ErrorStatus("HelloGrid: Arguments cannot be empty.");
return;
}
Index3 currentCell = new Index3();
int maxI = grid.NumCellsIJK.I;
int maxJ = grid.NumCellsIJK.J;
int maxK = grid.NumCellsIJK.K;
for (int i = 0; i < maxI; i++)
{
for (int j = 0; j < maxJ; j++)
{
for (int k = 0; k < maxK; k++)
{
currentCell.I = i; currentCell.J = j; currentCell.K = k;
if (grid.IsCellDefined(currentCell) && grid.HasCellVolume(currentCell))
{
//DictionaryProperty p = ???
//int val = p[currentCell] ???
}
}
}
}
You need to use the "FastDictionaryPropertyIndexer" or "FastPropertyIndexer" for regular properties.
foreach (var dictProp in grid.DictionaryProperties)
{
int numCellsI = dictProp.NumCellsIJK[0];
int numCellsJ = dictProp.NumCellsIJK[1];
int numCellsK = dictProp.NumCellsIJK[2];
float[] values = new float[dictProp.NumCells];
var dpsa = dictProp.SpecializedAccess;
using (var fdpi = dpsa.OpenFastDictionaryPropertyIndexer())
{
int index = 0;
for (int k = 0; k < numCellsK; k++)
{
for (int j = 0; j < numCellsJ; j++)
{
for (int i = 0; i < numCellsI; i++)
{
values[index] = fdpi[i, j, k];
index++;
}
}
}
}
}
You also need to be careful about the indexing since it varies by project. For instance, you may need to reverse the order of traversal in the J direction or you could end up with some strange results.

Dijkstras Algorithmn Vue

I am trying to implement dijkstras pathfinding algorithm in vue. I am following the psuedocode from https://medium.com/#nicholas.w.swift/easy-dijkstras-pathfinding-324a51eeb0f. This is what I have come up with so far, however I am struggling to translate the psuedocode into vue js. How would I be able to check every node and pop the visited nodes off the list?
Dijkstra() {
this.unexploredset = [];
for (let i = 0; i < 16; i++){
for (let j = 0; j < 16; j++){
this.nodes[i][j].position = '∞';
this.nodes[i][j].distance = '∞';
if(this.nodes[i][j].hasWall == false){
this.unexploredset.push(this.nodes[i][j])
}
}
}
let current = this.nodes[3][4];
let goal = this.nodes[14][14];
for(let i = 0; i < 255; i++) {
for (let k = 0; k < 4; k++) {
if (current.distance <= current.neighbours[k].distance && current.unvisited == true)
{
current.unvisited = false;
let temp = current.neighbours[k];
current = temp
this.unexploredset.pop(current);
current = temp
if (current == goal)
{
console.log("found");
break
}
console.log(this.unexploredset.length)
}
}
}
}

numWeights corresponding to mnumVertices?

I have one issue left with ASSIMP DIRECT X C++ ANIMATION WITH SKELETON.
for (UINT m = 0; m < currentMesh->mBones[k]->mNumWeights; m++) //verticer som påverkas
{
vertexVector[k].joints.x = currentMesh->mBones[k]->mWeights[m].mVertexId;
That code shows all vertices affected by a bone - k, inside an iteration.
All of these vertices must have the same vert ID since they are all affected by the same bone/joint.
The problem is, I need to make a list of every vertex and a list of every indice of a face, where I store position, UV, Normal etc.
The list that displays all of the vertices, is not in the same order obviously as the lists that displays all the vertices affected by each bone.
So how can I combine these lists?
"vertexVector"... etc is an example of a list with jointInfo that is corresponding to vertexID.
It has room for more places and another variable for the weight.
But that list doesn't work obviously.
What am I doing wrong with Assimp? Hope this was a clear post.
UPdate this is how i build the matrices: I don't know what is wrong.
void jointTransform(float
timeInSeconds, std::vector<DirectX::XMMATRIX>& transformM, aiAnimation*
ani, UINT nrOfJoints, std::vector<joints>& jointInfo, const aiScene*
scenePtr)
{
DirectX::XMMATRIX iD = DirectX::XMMatrixIdentity();
float ticksPerSecond = (float)ani->mTicksPerSecond;
if (ticksPerSecond == 0)
{
ticksPerSecond = 30;
}
float timeInTicks = timeInSeconds * ticksPerSecond;
float animationTime = fmod(timeInTicks, (float)ani->mDuration);
readNodeHeiarchy(animationTime, scenePtr->mRootNode, iD, jointInfo, ani,
scenePtr);
transformM.resize(nrOfJoints);
for (UINT i = 0; i < transformM.size(); i++)
{
transformM[i] = jointInfo[i].transformFinal;
}
}
void readNodeHeiarchy(float time, const aiNode* node, DirectX::XMMATRIX
parentMat, std::vector<joints>& jointInfo, aiAnimation* ani, const
aiScene* scenePtr)
{
std::string nodeNameString = node->mName.data;
//Skapa en parentTransform från noden. Som sedan skickas in som parent
matris, första gången är det identitetsmatrisen.
aiMatrix4x4 nodeTransform = node->mTransformation;
DirectX::XMMATRIX combined;
combined = DirectX::XMMatrixSet(nodeTransform.a1, nodeTransform.a2,
nodeTransform.a3, nodeTransform.a4,
nodeTransform.b1, nodeTransform.b2, nodeTransform.b3, nodeTransform.b4,
nodeTransform.c1, nodeTransform.c2, nodeTransform.c3, nodeTransform.c4,
nodeTransform.d1, nodeTransform.d2, nodeTransform.d3,
nodeTransform.d4);
const aiNodeAnim* joint = nullptr;
//Kolla om noden är ett ben.
for (UINT i = 0; i < ani->mNumChannels; i++)
{
if (nodeNameString == ani->mChannels[i]->mNodeName.data)
{
joint = ani->mChannels[i];
}
}
DirectX::XMMATRIX globalTransform = DirectX::XMMatrixIdentity();
//om den är ett ben så är joint inte längre nullptr, den blir det benet.
if (joint)
{
DirectX::XMMATRIX S;
DirectX::XMMATRIX R;
DirectX::XMMATRIX T;
//scale
aiVector3D scaleV;
calcLerpScale(scaleV, time, joint);
S = DirectX::XMMatrixScaling(scaleV.x, scaleV.y, scaleV.z);
//rotate
aiQuaternion rotationQ;
calcLerpRot(rotationQ, time, joint);
DirectX::XMVECTOR q;
q = DirectX::XMVectorSet(rotationQ.x, rotationQ.y, rotationQ.z,
rotationQ.w);
R = DirectX::XMMatrixRotationQuaternion(q);
//translate
aiVector3D transV;
calcLerpTrans(transV, time, joint);
T = DirectX::XMMatrixTranslation(transV.x, transV.y, transV.z);
combined = S * R * T;
globalTransform = combined * parentMat;
}
//DirectX::XMMATRIX globalTransform = combined * parentMat;
//if (jointInfo[jointInfo.size() - 1].name.C_Str() != nodeNameString)
//{
for (UINT i = 0; i < jointInfo.size(); i++)
{
if (jointInfo[i].name.C_Str() == nodeNameString)
{
OutputDebugStringA("\n");
OutputDebugStringA(jointInfo[i].name.C_Str());
OutputDebugStringA("\n");
aiMatrix4x4 off = jointInfo[i].offsetM;
DirectX::XMMATRIX offset;
offset = DirectX::XMMatrixSet(off.a1, off.a2, off.a3, off.a4,
off.b1, off.b2, off.b3, off.b4,
off.c1, off.c2, off.c3, off.c4,
off.d1, off.d2, off.d3, off.d4);
DirectX::XMMATRIX rootMInv;
aiMatrix4x4 rootInv = scenePtr->mRootNode-
>mTransformation.Inverse();
rootMInv = DirectX::XMMatrixSet(rootInv.a1, rootInv.a2,
rootInv.a3, rootInv.a4,
rootInv.b1, rootInv.b2, rootInv.b3, rootInv.b4,
rootInv.c1, rootInv.c2, rootInv.c3, rootInv.c4,
rootInv.d1, rootInv.d2, rootInv.d3, rootInv.d4);
jointInfo[i].transformFinal = offset * globalTransform *
rootMInv;
break;
}
}
//}
for (UINT i = 0; i < node->mNumChildren; i++)
{
readNodeHeiarchy(time, node->mChildren[i], globalTransform, jointInfo,
ani, scenePtr);
}
}
void calcLerpScale(aiVector3D& scale, float aniTime, const aiNodeAnim*
joint)
{
if (joint->mNumScalingKeys == 1)
{
scale = joint->mScalingKeys[0].mValue;
return;
}
UINT scaleInd = findIndexS(aniTime, joint);
UINT nextScale = scaleInd + 1;
assert(nextScale < joint->mNumScalingKeys);
float deltaTime = (float)joint->mScalingKeys[nextScale].mTime -
(float)joint->mScalingKeys[scaleInd].mTime;
float factor = (aniTime - (float)joint->mScalingKeys[scaleInd].mTime) /
deltaTime;
assert(factor >= 0.0f && factor <= 1.0f);
const aiVector3D& startScaleV = joint->mScalingKeys[scaleInd].mValue;
const aiVector3D& endScaleV = joint->mScalingKeys[nextScale].mValue;
//interpolate
aiVector3D Delta = endScaleV - startScaleV; // längden
scale = startScaleV + (factor * Delta); //gå ett antal steg beroende på
faktorn mellan start och slut.
scale.Normalize();
}
void calcLerpRot(aiQuaternion& rotation, float aniTime, const aiNodeAnim*
joint)
{
if (joint->mNumRotationKeys == 1)
{
rotation = joint->mRotationKeys[0].mValue;
return;
}
UINT rotIndex = findIndexRot(aniTime, joint);
UINT nextRot = (rotIndex + 1);
assert(nextRot < joint->mNumRotationKeys);
float deltaTime = (float)joint->mRotationKeys[nextRot].mTime -
(float)joint->mRotationKeys[rotIndex].mTime;
float factor = (aniTime - (float)joint->mRotationKeys[rotIndex].mTime) /
deltaTime;
assert(factor >= 0.0f && factor <= 1.0f);
const aiQuaternion& StartRotationQ = joint->mRotationKeys[rotIndex].mValue;
const aiQuaternion& EndRotationQ = joint->mRotationKeys[nextRot].mValue;
aiQuaternion::Interpolate(rotation, StartRotationQ, EndRotationQ, factor);
rotation.Normalize();
}
void calcLerpTrans(aiVector3D& translation, float aniTime, const
aiNodeAnim*
joint)
{
if (joint->mNumPositionKeys == 1)
{
translation = joint->mPositionKeys[0].mValue;
return;
}
UINT transIndex = findIndexT(aniTime, joint);
UINT nextTrans = (transIndex + 1);
assert(nextTrans < joint->mNumPositionKeys);
float deltaTime = (float)joint->mPositionKeys[nextTrans].mTime -
(float)joint->mPositionKeys[transIndex].mTime;
float factor = (aniTime - (float)joint->mPositionKeys[transIndex].mTime) /
deltaTime;
assert(factor >= 0.0f && factor <= 1.0f);
const aiVector3D& startTransV = joint->mPositionKeys[transIndex].mValue;
const aiVector3D& endTransV = joint->mPositionKeys[nextTrans].mValue;
//interpolate
aiVector3D Delta = endTransV - startTransV;
translation = startTransV + (factor * Delta);
translation.Normalize();
}
UINT findIndexRot(float aniTime, const aiNodeAnim* joint)
{
assert(joint->mNumRotationKeys > 0);
for (UINT i = 0; i < joint->mNumRotationKeys - 1; i++)
{
if (aniTime < (float)joint->mRotationKeys[i + 1].mTime)
{
return i;
}
}
assert(0);
}
}
Not sure what you mean by "All of these vertices must have the same vert ID" - the vertex id's of the k:th bone, according to mBones[k]->mWeights[..].mVertexId, are indices to vertices influenced by this bone, and they are going to be different (otherwise there would be either redundancy of conflict).
You probably want to have bone indices and bone weights as part of the vertex definition for easy handling in a shader. Something like
struct vertex (
vec3 pos;
vec3 normal;
float bone_weights[N]; // weights of bones influencing this vertex
unsigned bone_indices[N]; // indices of bones influencing this vertex
}
std::vector<vertex> mesh_vertices;
Where N is the maximum number of influence bones per vertex. A common value is four, but this depends on the mesh your are importing.
Based on your example, a rough draft could be something like this:
// k:th bone of bones in currentMesh
for (UINT m = 0; m < currentMesh->mBones[k]->mNumWeights; m++)
{
float bone_weight = currentMesh->mBones[k]->mWeights[m].mWeight;
unsigned vertex_index = currentMesh->mBones[k]->mWeights[m].mVertexId;
mesh_vertices[vertex_index].bone_weights[m] = bone_weight;
mesh_vertices[vertex_index].bone_indices[m] = k;
}
Here we've assumed that mNumWeights = N, but this needs to checked, as mentioned.

Microsoft Solver Foundation - Results Are Not Good Enough ( Stock Portfolio Optimization )

I'm trying to code a markowitz optimization class in C# but the optimization results not good enough. Portfolio weights are differing from matlab's and excel's solutions by average 0.2%. I checked my covariance matrix calculation and the other calculations and find that they are true. Is there a way to calibrate model's tolerance or something other for getting better results? There is my code.
public List<OptimalWeight> CalcOptimalWeights(bool isNegativeAllowed,string method)
{
List<OptimalWeight> weightsResult = new List<OptimalWeight>();
List<List<CovarItem>> covariances = new List<List<CovarItem>>();
covariances = CalcCovariances();
int n = this.AssetReturnList.Count();
SolverContext solver = SolverContext.GetContext();
Model model = solver.CreateModel();
if(isNegativeAllowed == false)
{
Decision[] weights = new Decision[n];
for (int i = 0; i < n; i++)
{
model.AddDecision(weights[i] = new Decision(Domain.RealNonnegative, null));
}
model.AddConstraint("SumWeights", Model.Sum(weights) == 1);
if(this.Constraints.Count() == 0)
{
if (method == "MinVar")
{
Term portVar = 0.0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
portVar += weights[j] * covariances[j][i].Covar * weights[i];
}
}
model.AddGoal("MinVarPort", GoalKind.Minimize, portVar);
Solution solution = solver.Solve();
var report = solution.GetReport();
var decisions = solution.Decisions;
List<double> d = decisions.Select(x => x.GetDouble()).ToList();
for(int i = 0 ; i < n; i++)
{
weightsResult.Add(new OptimalWeight {AssetId = AssetReturnList[i].AssetId,
Symbol = AssetReturnList[i].Symbol,
Weight = d[i] });
}
double pvar = solution.Goals.First().ToDouble();
}
}
}
return weightsResult;
}

2 dimensional List<T> in C++\CLI

I want to create a 2-dimensional List in C++\CLI. Question is how to declare it?
I have tried this:
List<List<int>^>^ H = gcnew List<List<int>>(); // Scoring matrix H
H->Add(gcnew List<int>() );
for (i = 0; i < n; i++) // Fill matrix H with 0
{
for (j = 0; j < m; j++)
{
H[i]->Add(0);
}
}
Then I get a lot of syntax errors, starting with this one:
error C3225: generic type argument for 'T' cannot be 'System::Collections::Generic::List', it must be a value type or a handle to a reference type
In this declaration
List<List<int>^>^ H = gcnew List<List<int>>();
The right type specifier does not correspond to the left type specifier. Should be
List<List<int>^>^ H = gcnew List<List<int>^>();
With advice from Hans and Vlad, this seems to work:
List<List<int>^>^ H = gcnew List<List<int>^>(); // Scoring matrix H
for (i = 0; i < n; i++) // Fill matrix H with 0
{
H->Add(gcnew List<int>() );
for (j = 0; j < m; j++)
{
H[i]->Add(0);
}
}
Thx, Jan