// ------------------------------------------------------------------------------------------------ // note: this functions works on 3D vectors, but performs its intersection checks solely in xy. bool PointInPoly(const IfcVector3& p, const std::vector<IfcVector3>& boundary) { // even-odd algorithm: take a random vector that extends from p to infinite // and counts how many times it intersects edges of the boundary. // because checking for segment intersections is prone to numeric inaccuracies // or double detections (i.e. when hitting multiple adjacent segments at their // shared vertices) we do it thrice with different rays and vote on it. // the even-odd algorithm doesn't work for points which lie directly on // the border of the polygon. If any of our attempts produces this result, // we return false immediately. std::vector<size_t> intersected_boundary_segments; std::vector<IfcVector3> intersected_boundary_points; size_t votes = 0; bool is_border; IntersectsBoundaryProfile(p, p + IfcVector3(1.0,0,0), boundary, intersected_boundary_segments, intersected_boundary_points, true, &is_border); if(is_border) { return false; } votes += intersected_boundary_segments.size() % 2; intersected_boundary_segments.clear(); intersected_boundary_points.clear(); IntersectsBoundaryProfile(p, p + IfcVector3(0,1.0,0), boundary, intersected_boundary_segments, intersected_boundary_points, true, &is_border); if(is_border) { return false; } votes += intersected_boundary_segments.size() % 2; intersected_boundary_segments.clear(); intersected_boundary_points.clear(); IntersectsBoundaryProfile(p, p + IfcVector3(0.6,-0.6,0.0), boundary, intersected_boundary_segments, intersected_boundary_points, true, &is_border); if(is_border) { return false; } votes += intersected_boundary_segments.size() % 2; //ai_assert(votes == 3 || votes == 0); return votes > 1; }
// ------------------------------------------------------------------------------------------------ void ConvertAxisPlacement(IfcMatrix4& out, const IfcAxis2Placement2D& in) { IfcVector3 loc; ConvertCartesianPoint(loc,in.Location); IfcVector3 x(1.f,0.f,0.f); if (in.RefDirection) { ConvertDirection(x,*in.RefDirection.Get()); } const IfcVector3 y = IfcVector3(x.y,-x.x,0.f); IfcMatrix4::Translation(loc,out); AssignMatrixAxes(out,x,y,IfcVector3(0.f,0.f,1.f)); }
// ------------------------------------------------------------------------------------------------ void ConvertCartesianPoint(IfcVector3& out, const IfcCartesianPoint& in) { out = IfcVector3(); for(size_t i = 0; i < in.Coordinates.size(); ++i) { out[i] = in.Coordinates[i]; } }
// ------------------------------------------------------------------------------------------------ void ConvertAxisPlacement(IfcVector3& axis, IfcVector3& pos, const IfcAxis1Placement& in) { ConvertCartesianPoint(pos,in.Location); if (in.Axis) { ConvertDirection(axis,in.Axis.Get()); } else { axis = IfcVector3(0.f,0.f,1.f); } }
// ------------------------------------------------------------------------------------------------ void TempMesh::ComputePolygonNormals(std::vector<IfcVector3>& normals, bool normalize, size_t ofs) const { size_t max_vcount = 0; std::vector<unsigned int>::const_iterator begin = vertcnt.begin()+ofs, end = vertcnt.end(), iit; for(iit = begin; iit != end; ++iit) { max_vcount = std::max(max_vcount,static_cast<size_t>(*iit)); } std::vector<IfcFloat> temp((max_vcount+2)*4); normals.reserve( normals.size() + vertcnt.size()-ofs ); // `NewellNormal()` currently has a relatively strange interface and need to // re-structure things a bit to meet them. size_t vidx = std::accumulate(vertcnt.begin(),begin,0); for(iit = begin; iit != end; vidx += *iit++) { if (!*iit) { normals.push_back(IfcVector3()); continue; } for(size_t vofs = 0, cnt = 0; vofs < *iit; ++vofs) { const IfcVector3& v = verts[vidx+vofs]; temp[cnt++] = v.x; temp[cnt++] = v.y; temp[cnt++] = v.z; #ifdef ASSIMP_BUILD_DEBUG temp[cnt] = std::numeric_limits<IfcFloat>::quiet_NaN(); #endif ++cnt; } normals.push_back(IfcVector3()); NewellNormal<4,4,4>(normals.back(),*iit,&temp[0],&temp[1],&temp[2]); } if(normalize) { BOOST_FOREACH(IfcVector3& n, normals) { n.Normalize(); } } }
// ------------------------------------------------------------------------------------------------ void ConvertDirection(IfcVector3& out, const IfcDirection& in) { out = IfcVector3(); for(size_t i = 0; i < in.DirectionRatios.size(); ++i) { out[i] = in.DirectionRatios[i]; } const IfcFloat len = out.Length(); if (len<1e-6) { IFCImporter::LogWarn("direction vector magnitude too small, normalization would result in a division by zero"); return; } out /= len; }
// ------------------------------------------------------------------------------------------------ void ProcessParametrizedProfile(const IfcParameterizedProfileDef& def, TempMesh& meshout, ConversionData& conv) { if(const IfcRectangleProfileDef* const cprofile = def.ToPtr<IfcRectangleProfileDef>()) { const IfcFloat x = cprofile->XDim*0.5f, y = cprofile->YDim*0.5f; meshout.verts.reserve(meshout.verts.size()+4); meshout.verts.push_back( IfcVector3( x, y, 0.f )); meshout.verts.push_back( IfcVector3(-x, y, 0.f )); meshout.verts.push_back( IfcVector3(-x,-y, 0.f )); meshout.verts.push_back( IfcVector3( x,-y, 0.f )); meshout.vertcnt.push_back(4); } else if( const IfcCircleProfileDef* const circle = def.ToPtr<IfcCircleProfileDef>()) { if( const IfcCircleHollowProfileDef* const hollow = def.ToPtr<IfcCircleHollowProfileDef>()) { // TODO } const size_t segments = 32; const IfcFloat delta = AI_MATH_TWO_PI_F/segments, radius = circle->Radius; meshout.verts.reserve(segments); IfcFloat angle = 0.f; for(size_t i = 0; i < segments; ++i, angle += delta) { meshout.verts.push_back( IfcVector3( ::cos(angle)*radius, ::sin(angle)*radius, 0.f )); } meshout.vertcnt.push_back(segments); } else { IFCImporter::LogWarn("skipping unknown IfcParameterizedProfileDef entity, type is " + def.GetClassName()); return; } IfcMatrix4 trafo; ConvertAxisPlacement(trafo, *def.Position); meshout.Transform(trafo); }
// ------------------------------------------------------------------------------------------------ void ConvertTransformOperator(IfcMatrix4& out, const IfcCartesianTransformationOperator& op) { IfcVector3 loc; ConvertCartesianPoint(loc,op.LocalOrigin); IfcVector3 x(1.f,0.f,0.f),y(0.f,1.f,0.f),z(0.f,0.f,1.f); if (op.Axis1) { ConvertDirection(x,*op.Axis1.Get()); } if (op.Axis2) { ConvertDirection(y,*op.Axis2.Get()); } if (const IfcCartesianTransformationOperator3D* op2 = op.ToPtr<IfcCartesianTransformationOperator3D>()) { if(op2->Axis3) { ConvertDirection(z,*op2->Axis3.Get()); } } IfcMatrix4 locm; IfcMatrix4::Translation(loc,locm); AssignMatrixAxes(out,x,y,z); IfcVector3 vscale; if (const IfcCartesianTransformationOperator3DnonUniform* nuni = op.ToPtr<IfcCartesianTransformationOperator3DnonUniform>()) { vscale.x = nuni->Scale?op.Scale.Get():1.f; vscale.y = nuni->Scale2?nuni->Scale2.Get():1.f; vscale.z = nuni->Scale3?nuni->Scale3.Get():1.f; } else { const IfcFloat sc = op.Scale?op.Scale.Get():1.f; vscale = IfcVector3(sc,sc,sc); } IfcMatrix4 s; IfcMatrix4::Scaling(vscale,s); out = locm * out * s; }
// ------------------------------------------------------------------------------------------------ void TempMesh::FixupFaceOrientation() { const IfcVector3 vavg = Center(); // create a list of start indices for all faces to allow random access to faces std::vector<size_t> faceStartIndices(vertcnt.size()); for( size_t i = 0, a = 0; a < vertcnt.size(); i += vertcnt[a], ++a ) faceStartIndices[a] = i; // list all faces on a vertex std::map<IfcVector3, std::vector<size_t>, CompareVector> facesByVertex; for( size_t a = 0; a < vertcnt.size(); ++a ) { for( size_t b = 0; b < vertcnt[a]; ++b ) facesByVertex[verts[faceStartIndices[a] + b]].push_back(a); } // determine neighbourhood for all polys std::vector<size_t> neighbour(verts.size(), SIZE_MAX); std::vector<size_t> tempIntersect(10); for( size_t a = 0; a < vertcnt.size(); ++a ) { for( size_t b = 0; b < vertcnt[a]; ++b ) { size_t ib = faceStartIndices[a] + b, nib = faceStartIndices[a] + (b + 1) % vertcnt[a]; const std::vector<size_t>& facesOnB = facesByVertex[verts[ib]]; const std::vector<size_t>& facesOnNB = facesByVertex[verts[nib]]; // there should be exactly one or two faces which appear in both lists. Our face and the other side std::vector<size_t>::iterator sectstart = tempIntersect.begin(); std::vector<size_t>::iterator sectend = std::set_intersection( facesOnB.begin(), facesOnB.end(), facesOnNB.begin(), facesOnNB.end(), sectstart); if( std::distance(sectstart, sectend) != 2 ) continue; if( *sectstart == a ) ++sectstart; neighbour[ib] = *sectstart; } } // now we're getting started. We take the face which is the farthest away from the center. This face is most probably // facing outwards. So we reverse this face to point outwards in relation to the center. Then we adapt neighbouring // faces to have the same winding until all faces have been tested. std::vector<bool> faceDone(vertcnt.size(), false); while( std::count(faceDone.begin(), faceDone.end(), false) != 0 ) { // find the farthest of the remaining faces size_t farthestIndex = SIZE_MAX; IfcFloat farthestDistance = -1.0; for( size_t a = 0; a < vertcnt.size(); ++a ) { if( faceDone[a] ) continue; IfcVector3 faceCenter = std::accumulate(verts.begin() + faceStartIndices[a], verts.begin() + faceStartIndices[a] + vertcnt[a], IfcVector3(0.0)) / IfcFloat(vertcnt[a]); IfcFloat dst = (faceCenter - vavg).SquareLength(); if( dst > farthestDistance ) { farthestDistance = dst; farthestIndex = a; } } // calculate its normal and reverse the poly if its facing towards the mesh center IfcVector3 farthestNormal = ComputePolygonNormal(verts.data() + faceStartIndices[farthestIndex], vertcnt[farthestIndex]); IfcVector3 farthestCenter = std::accumulate(verts.begin() + faceStartIndices[farthestIndex], verts.begin() + faceStartIndices[farthestIndex] + vertcnt[farthestIndex], IfcVector3(0.0)) / IfcFloat(vertcnt[farthestIndex]); // We accapt a bit of negative orientation without reversing. In case of doubt, prefer the orientation given in // the file. if( (farthestNormal * (farthestCenter - vavg).Normalize()) < -0.4 ) { size_t fsi = faceStartIndices[farthestIndex], fvc = vertcnt[farthestIndex]; std::reverse(verts.begin() + fsi, verts.begin() + fsi + fvc); std::reverse(neighbour.begin() + fsi, neighbour.begin() + fsi + fvc); // because of the neighbour index belonging to the edge starting with the point at the same index, we need to // cycle the neighbours through to match the edges again. // Before: points A - B - C - D with edge neighbour p - q - r - s // After: points D - C - B - A, reversed neighbours are s - r - q - p, but the should be // r q p s for( size_t a = 0; a < fvc - 1; ++a ) std::swap(neighbour[fsi + a], neighbour[fsi + a + 1]); } faceDone[farthestIndex] = true; std::vector<size_t> todo; todo.push_back(farthestIndex); // go over its neighbour faces recursively and adapt their winding order to match the farthest face while( !todo.empty() ) { size_t tdf = todo.back(); size_t vsi = faceStartIndices[tdf], vc = vertcnt[tdf]; todo.pop_back(); // check its neighbours for( size_t a = 0; a < vc; ++a ) { // ignore neighbours if we already checked them size_t nbi = neighbour[vsi + a]; if( nbi == SIZE_MAX || faceDone[nbi] ) continue; const IfcVector3& vp = verts[vsi + a]; size_t nbvsi = faceStartIndices[nbi], nbvc = vertcnt[nbi]; std::vector<IfcVector3>::iterator it = std::find_if(verts.begin() + nbvsi, verts.begin() + nbvsi + nbvc, FindVector(vp)); ai_assert(it != verts.begin() + nbvsi + nbvc); size_t nb_vidx = std::distance(verts.begin() + nbvsi, it); // two faces winded in the same direction should have a crossed edge, where one face has p0->p1 and the other // has p1'->p0'. If the next point on the neighbouring face is also the next on the current face, we need // to reverse the neighbour nb_vidx = (nb_vidx + 1) % nbvc; size_t oursideidx = (a + 1) % vc; if( FuzzyVectorCompare(1e-6)(verts[vsi + oursideidx], verts[nbvsi + nb_vidx]) ) { std::reverse(verts.begin() + nbvsi, verts.begin() + nbvsi + nbvc); std::reverse(neighbour.begin() + nbvsi, neighbour.begin() + nbvsi + nbvc); for( size_t a = 0; a < nbvc - 1; ++a ) std::swap(neighbour[nbvsi + a], neighbour[nbvsi + a + 1]); } // either way we're done with the neighbour. Mark it as done and continue checking from there recursively faceDone[nbi] = true; todo.push_back(nbi); } } // no more faces reachable from this part of the surface, start over with a disjunct part and its farthest face } }
// ------------------------------------------------------------------------------ IfcVector3 TempMesh::Center() const { return (verts.size() == 0) ? IfcVector3(0.0f, 0.0f, 0.0f) : (std::accumulate(verts.begin(),verts.end(),IfcVector3()) / static_cast<IfcFloat>(verts.size())); }
// ------------------------------------------------------------------------------------------------ void ProcessParametrizedProfile(const IfcParameterizedProfileDef& def, TempMesh& meshout, ConversionData& /*conv*/) { if(const IfcRectangleProfileDef* const cprofile = def.ToPtr<IfcRectangleProfileDef>()) { const IfcFloat x = cprofile->XDim*0.5f, y = cprofile->YDim*0.5f; meshout.verts.reserve(meshout.verts.size()+4); meshout.verts.push_back( IfcVector3( x, y, 0.f )); meshout.verts.push_back( IfcVector3(-x, y, 0.f )); meshout.verts.push_back( IfcVector3(-x,-y, 0.f )); meshout.verts.push_back( IfcVector3( x,-y, 0.f )); meshout.vertcnt.push_back(4); } else if( const IfcCircleProfileDef* const circle = def.ToPtr<IfcCircleProfileDef>()) { if(def.ToPtr<IfcCircleHollowProfileDef>()) { // TODO } const size_t segments = 32; const IfcFloat delta = AI_MATH_TWO_PI_F/segments, radius = circle->Radius; meshout.verts.reserve(segments); IfcFloat angle = 0.f; for(size_t i = 0; i < segments; ++i, angle += delta) { meshout.verts.push_back( IfcVector3( std::cos(angle)*radius, std::sin(angle)*radius, 0.f )); } meshout.vertcnt.push_back(segments); } else if( const IfcIShapeProfileDef* const ishape = def.ToPtr<IfcIShapeProfileDef>()) { // construct simplified IBeam shape const IfcFloat offset = (ishape->OverallWidth - ishape->WebThickness) / 2; const IfcFloat inner_height = ishape->OverallDepth - ishape->FlangeThickness * 2; meshout.verts.reserve(12); meshout.verts.push_back(IfcVector3(0,0,0)); meshout.verts.push_back(IfcVector3(0,ishape->FlangeThickness,0)); meshout.verts.push_back(IfcVector3(offset,ishape->FlangeThickness,0)); meshout.verts.push_back(IfcVector3(offset,ishape->FlangeThickness + inner_height,0)); meshout.verts.push_back(IfcVector3(0,ishape->FlangeThickness + inner_height,0)); meshout.verts.push_back(IfcVector3(0,ishape->OverallDepth,0)); meshout.verts.push_back(IfcVector3(ishape->OverallWidth,ishape->OverallDepth,0)); meshout.verts.push_back(IfcVector3(ishape->OverallWidth,ishape->FlangeThickness + inner_height,0)); meshout.verts.push_back(IfcVector3(offset+ishape->WebThickness,ishape->FlangeThickness + inner_height,0)); meshout.verts.push_back(IfcVector3(offset+ishape->WebThickness,ishape->FlangeThickness,0)); meshout.verts.push_back(IfcVector3(ishape->OverallWidth,ishape->FlangeThickness,0)); meshout.verts.push_back(IfcVector3(ishape->OverallWidth,0,0)); meshout.vertcnt.push_back(12); } else { IFCImporter::LogWarn("skipping unknown IfcParameterizedProfileDef entity, type is " + def.GetClassName()); return; } IfcMatrix4 trafo; ConvertAxisPlacement(trafo, *def.Position); meshout.Transform(trafo); }