Skip to content
Snippets Groups Projects
Commit bb15ca3d authored by Jie Cheng's avatar Jie Cheng
Browse files

Polygon tests for create model and create edge from points

parent 095cffeb
No related branches found
No related tags found
No related merge requests found
add_subdirectory(cxx)
if(SMTK_ENABLE_PYTHON_WRAPPING)
add_subdirectory(python)
endif()
#=============================================================================
#
# Copyright (c) Kitware, Inc.
# All rights reserved.
# See LICENSE.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even
# the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the above copyright notice for more information.
#
#=============================================================================
set (unit_tests
UnitTestPolygonCreateModel.cxx
UnitTestPolygonCreateEdgeFromPoints.cxx)
set (unit_tests_which_require_data)
set(external_libs ${Boost_LIBRARIES})
smtk_unit_tests(
LABEL "PolygonSession"
SOURCES ${unit_tests}
SOURCES_REQUIRE_DATA ${unit_tests_which_require_data}
LIBRARIES smtkCore smtkPolygonSession smtkCoreModelTesting ${external_libs}
)
//=========================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//=========================================================================
#include "smtk/attribute/Attribute.h"
#include "smtk/attribute/DoubleItem.h"
#include "smtk/attribute/GroupItem.h"
#include "smtk/attribute/IntItem.h"
#include "smtk/common/testing/cxx/helpers.h"
#include "smtk/model/CellEntity.h"
#include "smtk/model/Edge.h"
#include "smtk/model/Manager.h"
#include "smtk/model/Model.h"
#include "smtk/model/Operator.h"
#include "smtk/model/Session.h"
#include "smtk/model/Vertex.h"
#include <complex>
namespace
{
static const double tolerance = 1.e-5;
}
int UnitTestPolygonCreateEdgeFromPoints(int argc, char* argv[])
{
(void)argc;
(void)argv;
// In this test, 3 two-dimensional points are going to be created
const int numCoordsPerPoint = 2;
const std::vector<double> points{ -0.5, -0.5, 0.5, -0.5, 0.5, 0.5 };
const int numPoints = static_cast<int>(points.size()) / numCoordsPerPoint;
smtk::model::ManagerPtr manager = smtk::model::Manager::create();
smtk::model::SessionRef session = manager->createSession("polygon");
smtk::model::OperatorPtr myOp = session.op("create model");
smtk::model::OperatorResult res = myOp->operate();
smtk::model::EntityRef myModel = res->findModelEntity("created")->value();
// Associate model with operation
myOp = session.op("create edge from points");
test(myOp != nullptr, "No create edge from points operator");
test(myOp->specification()->associateEntity(myModel), "Could not associate model");
// Specify the points
std::cout << "Creating edge using (-0.5, -0.5), (0.5, -0.5) and (0.5, 0.5)" << std::endl;
smtk::attribute::IntItemPtr pointGeometry = myOp->specification()->findInt("pointGeometry");
test(pointGeometry != nullptr, "Could not find pointGeometry");
test(pointGeometry->setValue(numCoordsPerPoint), "Could not set pointGeometry");
smtk::attribute::GroupItem::Ptr pointsInfo = myOp->specification()->findGroup("2DPoints");
test(pointsInfo->setNumberOfGroups(numPoints), "Could not set number of points");
for (int i = 0; i < numPoints; ++i)
{
smtk::attribute::ItemPtr point = pointsInfo->find(i, "points");
test(point != nullptr, "Could not find point");
for (int j = 0; j < numCoordsPerPoint; ++j)
{
test(smtk::dynamic_pointer_cast<smtk::attribute::DoubleItem>(point)->setValue(
j, points[i * numCoordsPerPoint + j]),
"Setting points failed");
}
}
// Apply the operation
res = myOp->operate();
test(res->findInt("outcome")->value() == smtk::model::OPERATION_SUCCEEDED,
"Create edge from points operator failed");
// Check the created edge and vertices
smtk::model::Model modelCreated = static_cast<smtk::model::Model>(myModel);
smtk::model::Edges edges;
smtk::model::Vertices verts;
for (auto& cell : modelCreated.cells())
{
if (smtk::model::isEdge(cell.entityFlags()))
{
smtk::model::Edge e = static_cast<smtk::model::Edge>(cell);
edges.push_back(e);
smtk::model::Vertices vertsOnEdge = e.vertices();
verts.insert(verts.end(), vertsOnEdge.begin(), vertsOnEdge.end());
}
}
test(edges.size() == 1, "Incorrect number of edges");
test(verts.size() == edges.size() * numCoordsPerPoint, "Incorrect number of vertices");
test(std::abs(verts[0].coordinates()[0] + 0.5) < tolerance, "Incorrect coordinates of vertex");
test(std::abs(verts[0].coordinates()[1] + 0.5) < tolerance, "Incorrect coordinates of vertex");
test(std::abs(verts[1].coordinates()[0] - 0.5) < tolerance, "Incorrect coordinates of vertex");
test(std::abs(verts[1].coordinates()[1] - 0.5) < tolerance, "Incorrect coordinates of vertex");
return 0;
}
// This macro ensures the polygon session library is loaded into the executable
smtkComponentInitMacro(smtk_polygon_session)
//=========================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//=========================================================================
#include "smtk/attribute/DoubleItem.h"
#include "smtk/attribute/IntItem.h"
#include "smtk/attribute/SearchStyle.h"
#include "smtk/attribute/StringItem.h"
#include "smtk/common/testing/cxx/helpers.h"
#include "smtk/model/FloatData.h"
#include "smtk/model/Manager.h"
#include "smtk/model/Operator.h"
#include "smtk/model/Session.h"
#include <complex>
namespace
{
static const double tolerance = 1.e-5;
}
int UnitTestPolygonCreateModel(int argc, char* argv[])
{
(void)argc;
(void)argv;
smtk::model::ManagerPtr manager = smtk::model::Manager::create();
std::cout << "Available sessions\n";
smtk::model::StringList sessions = manager->sessionTypeNames();
for (smtk::model::StringList::iterator it = sessions.begin(); it != sessions.end(); ++it)
std::cout << " " << *it << "\n";
std::cout << "\n";
smtk::model::SessionRef session = manager->createSession("polygon");
std::cout << "Available operators\n";
smtk::model::StringList opnames = session.operatorNames();
for (smtk::model::StringList::iterator it = opnames.begin(); it != opnames.end(); ++it)
std::cout << " " << *it << "\n";
std::cout << "\n";
smtk::model::OperatorPtr createOp = session.op("create model");
test(createOp != nullptr, "No create operator");
// Specify model name, origin, x axis, y axis and feature size
smtk::attribute::StringItemPtr name = createOp->specification()->findString("name");
test(name != nullptr, "Name attribute not found");
name->setIsEnabled(true);
test(name->setValue("my model"), "Could not set name attribute");
smtk::attribute::IntItemPtr constructionMethod =
createOp->specification()->findInt("construction method");
test(constructionMethod != nullptr, "Construction method not found");
test(constructionMethod->setValue(0), "Could not set construction method");
int numActiveChildren = static_cast<int>(constructionMethod->numberOfActiveChildrenItems());
std::cout << "Available active children items: " << std::endl;
for (int i = 0; i < numActiveChildren; ++i)
{
smtk::attribute::ItemPtr item = constructionMethod->activeChildItem(i);
std::cout << item->name() << std::endl;
}
smtk::attribute::ItemPtr origin =
constructionMethod->findChild("origin", smtk::attribute::SearchStyle::ACTIVE_CHILDREN);
test(origin != nullptr, "Origin not found");
double originCoords[] = { 1., 2., 3. };
for (int i = 0; i < 3; ++i)
{
test(
smtk::dynamic_pointer_cast<smtk::attribute::DoubleItem>(origin)->setValue(i, originCoords[i]),
"Setting origin failed");
}
smtk::attribute::ItemPtr xAxis =
constructionMethod->findChild("x axis", smtk::attribute::SearchStyle::ACTIVE_CHILDREN);
test(xAxis != nullptr, "x axis not found");
double vx[] = { 0.707107, 0.707107, 0. };
for (int i = 0; i < 3; ++i)
{
test(smtk::dynamic_pointer_cast<smtk::attribute::DoubleItem>(xAxis)->setValue(i, vx[i]),
"Setting x axis failed");
}
smtk::attribute::ItemPtr yAxis =
constructionMethod->findChild("y axis", smtk::attribute::SearchStyle::ACTIVE_CHILDREN);
test(yAxis != nullptr, "y axis not found");
double vy[] = { -0.707107, 0.707107, 0. };
for (int i = 0; i < 3; ++i)
{
test(smtk::dynamic_pointer_cast<smtk::attribute::DoubleItem>(yAxis)->setValue(i, vy[i]),
"Setting y axis failed");
}
smtk::attribute::ItemPtr featureSize =
constructionMethod->findChild("feature size", smtk::attribute::SearchStyle::ACTIVE_CHILDREN);
test(featureSize != nullptr, "feature size not found");
test(smtk::dynamic_pointer_cast<smtk::attribute::DoubleItem>(featureSize)->setValue(1.),
"Setting feature size failed");
// Apply the operation and check the result
smtk::model::OperatorResult createOpResult = createOp->operate();
test(createOpResult->findInt("outcome")->value() == smtk::model::OPERATION_SUCCEEDED,
"Create operator failed");
smtk::attribute::ModelEntityItemPtr modelptr = createOpResult->findModelEntity("created");
test(modelptr != nullptr, "Could not find the created model");
smtk::model::EntityRef myModel = modelptr->value();
// Verify the name, origin, x axis, y axis, feature size
std::cout << "Model name: " << myModel.name() << std::endl;
test(myModel.name() == "my model", "Incorrect model name");
std::cout << "Model origin: ";
smtk::model::FloatList modelOrigin = myModel.floatProperty("origin");
for (int i = 0; i < 3; ++i)
{
std::cout << modelOrigin[i] << " ";
test(std::abs(modelOrigin[i] - originCoords[i]) < tolerance, "Incorrect model origin");
}
std::cout << "\nModel x axis: ";
smtk::model::FloatList modelXAxis = myModel.floatProperty("x axis");
for (int i = 0; i < 3; ++i)
{
std::cout << modelXAxis[i] << " ";
test(std::abs(modelXAxis[i] - vx[i]) < tolerance, "Incorrect model x axis");
}
std::cout << "\nModel y axis: ";
smtk::model::FloatList modelYAxis = myModel.floatProperty("y axis");
for (int i = 0; i < 3; ++i)
{
std::cout << modelYAxis[i] << " ";
test(std::abs(modelYAxis[i] - vy[i]) < tolerance, "Incorrect model y axis");
}
std::cout << std::endl;
return 0;
}
// This macro ensures the polygon session library is loaded into the executable
smtkComponentInitMacro(smtk_polygon_session)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment