Hide menu
Loading...
Searching...
No Matches
exploring/mesh/main.cxx

Refer to the mtk_mesh_representation_example_page.

// ****************************************************************************
// $Id$
//
// Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
// Copyright (C) 2014-2025, CADEX. All rights reserved.
//
// This file is part of the Manufacturing Toolkit software.
//
// You may use this file under the terms of the BSD license as follows:
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// ****************************************************************************
#include <cadex/LicenseManager_Activate.h>
#include <cadex/ModelAlgo/MeshGenerator.hxx>
#include <cadex/ModelData/IndexedTriangleSet.hxx>
#include <cadex/ModelData/MeshBody.hxx>
#include <cadex/ModelData/Model.hxx>
#include <cadex/ModelData/ModelReader.hxx>
#include <cadex/ModelData/ModelReaderParameters.hxx>
#include <cadex/ModelData/Part.hxx>
#include <cadex/ModelData/PointSet.hxx>
#include <cadex/ModelData/Polyline2dSet.hxx>
#include <cadex/ModelData/PolylineSet.hxx>
#include <cadex/ModelData/ShapeIterator.hxx>
#include <cadex/ModelData/ModelElementIterator.hxx>
#include <iostream>
#include "../../mtk_license.cxx"
using namespace std;
using namespace cadex;
static void DumpPointSet (const ModelData::PointSet& thePS)
{
const auto& aNumberOfPoints = thePS.NumberOfPoints();
cout << "PointSet: " << aNumberOfPoints << " points" << endl;
for (size_t i = 0; i < aNumberOfPoints; ++i) {
cout << "Point " << i << ":";
const auto& aP = thePS.Point (i);
cout << "(" << aP.X() << ", " << aP.Y() << ", " << aP.Z() << ")" << endl;
}
}
static void DumpPolylineSet (const ModelData::PolylineSet& thePLS)
{
const auto& aNumberOfPolylines = thePLS.NumberOfPolylines();
cout << "PolyineSet: " << aNumberOfPolylines << " polylines" << endl;
for (size_t i = 0; i < aNumberOfPolylines; ++i) {
cout << "Polyline " << i << ":" << endl;
cout << " Node coordinates:" << endl;
for (size_t j = 0; j < thePLS.Polyline (i).NumberOfPoints(); ++j) {
const auto& aP = thePLS.Polyline (i).Point (j);
cout << "(" << aP.X() << ", " << aP.Y() << ", " << aP.Z() << ")" << endl;
}
}
}
static void DumpPolyline2dSet (const ModelData::Polyline2dSet& thePLS)
{
const auto& aNumberOfPolylines2d = thePLS.NumberOfPolylines();
cout << "Polyine2dSet: " << aNumberOfPolylines2d << " polylines" << endl;
for (size_t i = 0; i < aNumberOfPolylines2d; ++i) {
cout << "Polyline2d " << i << ":" << endl;
cout << " Node coordinates:" << endl;
for (size_t j = 0; j < thePLS.Polyline (i).NumberOfPoints(); ++j) {
const auto& aP = thePLS.Polyline (i).Point (j);
cout << "(" << aP.X() << ", " << aP.Y() << ")" << endl;
}
}
}
static void DumpTriangleSet (const ModelData::IndexedTriangleSet& theITS)
{
const auto& aNumberOfTriangles = theITS.NumberOfTriangles();
cout << "IndexedTriangleSet: " << aNumberOfTriangles << " triangles:" << endl;
for (size_t i = 0; i < aNumberOfTriangles; ++i) {
cout << "Triangle " << i << ":" << endl;
for (size_t j = 0; j < 3; ++j) {
cout << " Node " << j << ": ";
// Coordinates
const auto& aVI = theITS.TriangleVertexIndex (i, j);
const auto& aV = theITS.TriangleVertex (i, j);
cout << "Vertex " << aVI << " (" << aV.X() << ", " << aV.Y() << ", " << aV.Z() << ")" << endl;
// Normals
if (theITS.HasNormals()) {
const auto& aN = theITS.TriangleVertexNormal (i, j);
cout << "Normal: (" << aN.X() << ", " << aN.Y() << ", " << aN.Z() << ")" << endl;
}
}
}
}
static void PrintMeshShapeInfo (const ModelData::MeshShape& theMeshShape)
{
if (ModelData::IndexedTriangleSet::CompareType (theMeshShape)) {
cout << " IndexedTriangleSet type." << endl;
const auto& anITS = static_cast<const ModelData::IndexedTriangleSet&> (theMeshShape);
DumpTriangleSet (anITS);
} else if (ModelData::PolylineSet::CompareType (theMeshShape)) {
cout << " PolyLineSet type" << endl;
const auto& aPLS = static_cast<const ModelData::PolylineSet&> (theMeshShape);
DumpPolylineSet (aPLS);
} else if (ModelData::Polyline2dSet::CompareType (theMeshShape)) {
cout << " PolyLine2dSet type" << endl;
const auto& aPL2dS = static_cast<const ModelData::Polyline2dSet&> (theMeshShape);
DumpPolyline2dSet (aPL2dS);
} else if (ModelData::PointSet::CompareType (theMeshShape)) {
cout << " PolyPointSet type" << endl;
const auto& aPPS = static_cast<const ModelData::PointSet&> (theMeshShape);
DumpPointSet (aPPS);
} else {
cout << " Undefined type" << endl;
}
}
static void ExploreMeshBodies (const std::vector<ModelData::Body>& theBodies)
{
for (size_t i = 0; i < theBodies.size(); ++i) {
const auto& aBody = theBodies[i];
if (ModelData::MeshBody::CompareType (aBody)) {
const auto& aMeshBody = static_cast<const ModelData::MeshBody&> (theBodies[i]);
cout << "MeshBody " << i << endl;
const auto& aMeshShapes = aMeshBody.Shapes();
// Iterate over MeshShape vector
for (size_t j = 0; j < aMeshShapes.size(); j++) {
const auto& aMeshShape = aMeshShapes[j];
cout << "MeshShape " << j;
PrintMeshShapeInfo (aMeshShape);
}
}
}
}
class PartMeshVisitor : public ModelData::ModelElementVoidVisitor
{
protected:
void operator() (const ModelData::Part& thePart) override
{
cout << "Part = \"" << thePart.Name() << "\"" << endl;
const auto& aBodies = thePart.Bodies();
if (!aBodies.empty()) {
ExploreMeshBodies (aBodies);
}
}
};
int main (int argc, char* argv[])
{
auto aKey = MTKLicenseKey::Value();
// Activate the license (aKey must be defined in mtk_license.cxx)
if (!CADExLicense_Activate (aKey)) {
cerr << "Failed to activate Manufacturing Toolkit license." << endl;
return 1;
}
// Get the input
if (argc != 2) {
cerr << "Usage: " << argv[0] << " <input_file>, where:" << endl;
cerr << " <input_file> is a name of the file to be read" << endl;
return 1;
}
const char* aSource = argv[1];
// Open the model
if (!aReader.Read (aSource, aModel)) {
cerr << "Failed to read the file " << aSource << endl;
return 1;
}
// Explore mesh bodies of model parts
PartMeshVisitor aVisitor;
aModel.Accept (aVisitor);
return 0;
}
const PointType & Point(size_t theIndex) const
Returns the point at position theIndex in polyline.
Definition Polyline2d.cxx:83
const PointType & Point(size_t theIndex) const
Returns the point at position theIndex in polyline.
Definition Polyline.cxx:83
Defines a polygonal shape consisting of triangles.
Definition IndexedTriangleSet.hxx:33
bool HasNormals() const
Returns true if the triangle set has explicitly defined normals.
Definition IndexedTriangleSet.cxx:213
size_t NumberOfTriangles() const
Returns a number of triangles.
Definition IndexedTriangleSet.cxx:326
const VertexType & TriangleVertex(size_t theTriangleIndex, size_t theVertexSlot) const
Returns a vertex of a triangle.
Definition IndexedTriangleSet.cxx:241
const NormalType & TriangleVertexNormal(size_t theTriangleIndex, size_t theVertexSlot) const
Returns a normal at vertex in a triangle.
Definition IndexedTriangleSet.cxx:273
int TriangleVertexIndex(size_t theTriangleIndex, size_t theVertexSlot) const
Returns a vertex index in a triangle.
Definition IndexedTriangleSet.cxx:254
Defines a body that represents a polygonal mesh (faceted or tessellated) of .
Definition MeshBody.hxx:32
Base class for all polygonal geometry containers.
Definition MeshShape.hxx:29
UTF16String Name() const
Returns a name.
Definition ModelElement.cxx:55
Element visitor with empty implementation.
Definition ModelElementVisitor.hxx:64
Provides MTK data model.
Definition Model.hxx:40
void Accept(ModelElementVisitor &theVisitor) const
Accepts a visitor.
Definition Model.cxx:274
Reads supported formats, see Import section.
Definition ModelReader.hxx:33
bool Read(const UTF16String &theFilePath, ModelData::Model &theModel)
Reads the file at the specified path into the specified model.
Definition ModelReader.cxx:266
Defines parameters of the ModelReader.
Definition ModelReaderParameters.hxx:29
Defines a leaf node in the scene graph hiearchy.
Definition Part.hxx:34
Defines a polygonal shape consisting of individual points.
Definition PointSet.hxx:32
size_t NumberOfPoints() const
Returns a number of points in point set.
Definition PointSet.cxx:67
const Geom::Point & Point(size_t theIndex) const
Returns the point at position theIndex in point set.
Definition PointSet.cxx:61
Defines a polygonal shape consisting of polylines.
Definition Polyline2dSet.hxx:32
const Geom::Polyline2d & Polyline(size_t theIndex) const
Returns the polyline at position theIndex in polyline set.
Definition Polyline2dSet.cxx:64
size_t NumberOfPolylines() const
Returns a number of polylines in polyline set.
Definition Polyline2dSet.cxx:70
Defines a polygonal shape consisting of polylines.
Definition PolylineSet.hxx:32
size_t NumberOfPolylines() const
Returns a number of polylines in polyline set.
Definition PolylineSet.cxx:72
const Geom::Polyline & Polyline(size_t theIndex) const
Returns the polyline at position theIndex in polyline set.
Definition PolylineSet.cxx:66
Contains classes, namespaces, enums, types, and global functions related to Manufacturing Toolkit.
Definition LicenseManager_LicenseError.hxx:30