//! ---|> NormalAttributeAccessor void setNormal(uint32_t index, const Geometry::Vec3 & n) override { assertRange(index); int8_t * v = _ptr<int8_t>(index); v[0] = Geometry::Convert::toSigned<int8_t>(n.x()); v[1] = Geometry::Convert::toSigned<int8_t>(n.y()); v[2] = Geometry::Convert::toSigned<int8_t>(n.z()); }
void drawVector(RenderingContext & rc, const Geometry::Vec3 & from, const Geometry::Vec3 & to) { static Util::Reference<Mesh> mesh; if (mesh.isNull()) { VertexDescription vertexDescription; vertexDescription.appendPosition3D(); mesh = new Mesh(vertexDescription, 2, 2); mesh->setDrawMode(Mesh::DRAW_LINES); MeshIndexData & id = mesh->openIndexData(); uint32_t * indices = id.data(); indices[0] = 0; indices[1] = 1; id.updateIndexRange(); id.markAsChanged(); mesh->setDataStrategy(SimpleMeshDataStrategy::getPureLocalStrategy()); } MeshVertexData & vd = mesh->openVertexData(); float * vertices = reinterpret_cast<float *> (vd.data()); *vertices++ = from.getX(); // From *vertices++ = from.getY(); *vertices++ = from.getZ(); *vertices++ = to.getX(); // To *vertices++ = to.getY(); *vertices++ = to.getZ(); vd.updateBoundingBox(); vd.markAsChanged(); rc.displayMesh(mesh.get()); }
static void describeGeometryNode(ExporterContext & /*ctxt*/,DescriptionMap & desc, Node * node) { desc.setString(Consts::ATTR_NODE_TYPE, Consts::NODE_TYPE_GEOMETRY); std::unique_ptr<DescriptionMap> dataDesc(new DescriptionMap); GeometryNode * gn = dynamic_cast<GeometryNode*>(node); // annotate with bounding box const Geometry::Box bb = gn->getBB(); const Geometry::Vec3 center = bb.getCenter(); std::stringstream s; s << center.getX() << " " << center.getY() << " " << center.getZ() << " " << bb.getExtentX() << " " << bb.getExtentY() << " " << bb.getExtentZ(); dataDesc->setString(Consts::ATTR_MESH_BB, s.str()); Rendering::Mesh * m = gn->getMesh(); if(m!=nullptr) { // mesh present? // no filename -> store data in .minsg if(m->getFileName().empty()) { std::ostringstream meshStream; if(Rendering::Serialization::saveMesh(gn->getMesh(), "mmf", meshStream)) { dataDesc->setString(Consts::ATTR_DATA_TYPE,"mesh"); dataDesc->setString(Consts::ATTR_DATA_ENCODING,"base64"); const std::string streamString = meshStream.str(); const std::string meshString = Util::encodeBase64(std::vector<uint8_t>(streamString.begin(), streamString.end())); dataDesc->setString(Consts::DATA_BLOCK,meshString); } } else { // filename given? Util::FileName meshFilename(m->getFileName()); // // make path to mesh relative to scene (if mesh lies below the scene) // Util::FileUtils::makeRelativeIfPossible(ctxt.sceneFile, meshFilename); dataDesc->setString(Consts::ATTR_DATA_TYPE,"mesh"); dataDesc->setString(Consts::ATTR_MESH_FILENAME,meshFilename.toShortString()); } ExporterTools::addDataEntry(desc, std::move(dataDesc)); } }
//! ---|> NormalAttributeAccessor void setNormal(uint32_t index, const Geometry::Vec3 & n) override { assertRange(index); float * v = _ptr<float>(index); v[0] = n.x() , v[1] = n.y() , v[2] = n.z(); }
void BoxTest::testMisc() { const Geometry::Box b1(-1.0f, 1.0f, -2.0f, 2.0f, -3.0f, 3.0f); Geometry::Box b2; b2 = Geometry::Box(); CPPUNIT_ASSERT(b2.isValid()); b2.invalidate(); CPPUNIT_ASSERT(b2.isInvalid()); CPPUNIT_ASSERT(b1.getCorner(Geometry::corner_t::xyz) == Geometry::Vec3(b1.getMinX(), b1.getMinY(), b1.getMinZ())); CPPUNIT_ASSERT(b1.getCorner(Geometry::corner_t::Xyz) == Geometry::Vec3(b1.getMaxX(), b1.getMinY(), b1.getMinZ())); CPPUNIT_ASSERT(b1.getCorner(Geometry::corner_t::xYz) == Geometry::Vec3(b1.getMinX(), b1.getMaxY(), b1.getMinZ())); CPPUNIT_ASSERT(b1.getCorner(Geometry::corner_t::XYz) == Geometry::Vec3(b1.getMaxX(), b1.getMaxY(), b1.getMinZ())); CPPUNIT_ASSERT(b1.getCorner(Geometry::corner_t::xyZ) == Geometry::Vec3(b1.getMinX(), b1.getMinY(), b1.getMaxZ())); CPPUNIT_ASSERT(b1.getCorner(Geometry::corner_t::XyZ) == Geometry::Vec3(b1.getMaxX(), b1.getMinY(), b1.getMaxZ())); CPPUNIT_ASSERT(b1.getCorner(Geometry::corner_t::xYZ) == Geometry::Vec3(b1.getMinX(), b1.getMaxY(), b1.getMaxZ())); CPPUNIT_ASSERT(b1.getCorner(Geometry::corner_t::XYZ) == Geometry::Vec3(b1.getMaxX(), b1.getMaxY(), b1.getMaxZ())); CPPUNIT_ASSERT(Geometry::Box::getOppositeCorner(Geometry::corner_t::xyz) == Geometry::corner_t::XYZ); CPPUNIT_ASSERT(Geometry::Box::getOppositeCorner(Geometry::corner_t::Xyz) == Geometry::corner_t::xYZ); CPPUNIT_ASSERT(Geometry::Box::getOppositeCorner(Geometry::corner_t::xYz) == Geometry::corner_t::XyZ); CPPUNIT_ASSERT(Geometry::Box::getOppositeCorner(Geometry::corner_t::XYz) == Geometry::corner_t::xyZ); CPPUNIT_ASSERT(Geometry::Box::getOppositeCorner(Geometry::corner_t::xyZ) == Geometry::corner_t::XYz); CPPUNIT_ASSERT(Geometry::Box::getOppositeCorner(Geometry::corner_t::XyZ) == Geometry::corner_t::xYz); CPPUNIT_ASSERT(Geometry::Box::getOppositeCorner(Geometry::corner_t::xYZ) == Geometry::corner_t::Xyz); CPPUNIT_ASSERT(Geometry::Box::getOppositeCorner(Geometry::corner_t::XYZ) == Geometry::corner_t::xyz); CPPUNIT_ASSERT(Geometry::Helper::getNormal(Geometry::side_t::X_NEG) == Geometry::Vec3(-1.0f, 0.0f, 0.0f)); CPPUNIT_ASSERT(Geometry::Helper::getNormal(Geometry::side_t::X_POS) == Geometry::Vec3(1.0f, 0.0f, 0.0f)); CPPUNIT_ASSERT(Geometry::Helper::getNormal(Geometry::side_t::Y_NEG) == Geometry::Vec3(0.0f, -1.0f, 0.0f)); CPPUNIT_ASSERT(Geometry::Helper::getNormal(Geometry::side_t::Y_POS) == Geometry::Vec3(0.0f, 1.0f, 0.0f)); CPPUNIT_ASSERT(Geometry::Helper::getNormal(Geometry::side_t::Z_NEG) == Geometry::Vec3(0.0f, 0.0f, -1.0f)); CPPUNIT_ASSERT(Geometry::Helper::getNormal(Geometry::side_t::Z_POS) == Geometry::Vec3(0.0f, 0.0f, 1.0f)); for (uint_fast8_t s = 0; s < 6; ++s) { const Geometry::side_t side = static_cast<const Geometry::side_t>(s); const Geometry::corner_t * corners = Geometry::Helper::getCornerIndices(side); for (uint_fast8_t i = 0; i < 4; ++i) { const uint_fast8_t prevCorner = i % 4; const uint_fast8_t currentCorner = (i + 1) % 4; const uint_fast8_t nextCorner = (i + 2) % 4; const Geometry::Vec3 edgeA = b1.getCorner(corners[nextCorner]) - b1.getCorner(corners[currentCorner]); const Geometry::Vec3 edgeB = b1.getCorner(corners[prevCorner]) - b1.getCorner(corners[currentCorner]); Geometry::Vec3 normal = edgeA.cross(edgeB); normal.normalize(); CPPUNIT_ASSERT(Geometry::Helper::getNormal(side) == normal); } } const Geometry::Vec3 v1(-1.1f, 0.0f, 0.0f); const Geometry::Vec3 v2(0.0f, 0.0f, 0.0f); const Geometry::Vec3 v3(1.1f, 0.0f, 0.0f); CPPUNIT_ASSERT(!b1.contains(-1.1f, 0.0f, 0.0f)); CPPUNIT_ASSERT(b1.contains(0.0f, 0.0f, 0.0f)); CPPUNIT_ASSERT(!b1.contains(1.1f, 0.0f, 0.0f)); CPPUNIT_ASSERT(!b1.contains(v1)); CPPUNIT_ASSERT(b1.contains(v2)); CPPUNIT_ASSERT(!b1.contains(v3)); CPPUNIT_ASSERT( Geometry::Intersection::isBoxIntersectingTriangle(b1, Geometry::Triangle<Geometry::Vec3>(v1, v2, v3))); CPPUNIT_ASSERT(b1.contains(Geometry::Box())); CPPUNIT_ASSERT(b1.contains(b1)); CPPUNIT_ASSERT(!b1.contains(Geometry::Box(-1.1f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f))); CPPUNIT_ASSERT(Geometry::Intersection::isBoxIntersectingBox(Geometry::Box(0, 1, 0, 1, 0, 1), Geometry::Box(0, 1, 0, 1, 0, 1))); CPPUNIT_ASSERT(!Geometry::Intersection::isBoxIntersectingBox(Geometry::Box(0, 1, 0, 1, 0, 1), Geometry::Box(2, 3, 2, 3, 2, 3))); CPPUNIT_ASSERT(!Geometry::Intersection::isBoxIntersectingBox(Geometry::Box(0, 1, 0, 1, 0, 1), Geometry::Box(0, 3, 2, 3, 2, 3))); CPPUNIT_ASSERT(!Geometry::Intersection::isBoxIntersectingBox(Geometry::Box(0, 1, 0, 1, 0, 1), Geometry::Box(2, 3, 0, 3, 2, 3))); CPPUNIT_ASSERT(!Geometry::Intersection::isBoxIntersectingBox(Geometry::Box(0, 1, 0, 1, 0, 1), Geometry::Box(2, 3, 2, 3, 0, 3))); CPPUNIT_ASSERT(Geometry::Intersection::isBoxIntersectingBox(Geometry::Box(0, 1, 0, 1, 0, 1), Geometry::Box(-1, 2, -1, 2, -1, 2))); CPPUNIT_ASSERT( Geometry::Intersection::isBoxIntersectingBox(b1, Geometry::Box(-1.1f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f))); CPPUNIT_ASSERT( !Geometry::Intersection::isBoxIntersectingBox(b1, Geometry::Box(-1.1f, -1.0f, -2.1f, -2.0f, -3.0f, -3.0f))); b2 = Geometry::Box(0.5f, 1.5f, 1.5f, 2.5f, 2.5f, 3.5f); CPPUNIT_ASSERT(Geometry::Intersection::isBoxIntersectingBox(b1, b2)); CPPUNIT_ASSERT(Geometry::Intersection::getBoxBoxIntersection(b1, b2) == Geometry::Box(0.5f, 1.0f, 1.5f, 2.0f, 2.5f, 3.0f)); CPPUNIT_ASSERT(b1.getDistance(Geometry::Vec3(-2.0f, 0.0f, 0.0f)) == 1.0f); CPPUNIT_ASSERT(b1.getDistance(Geometry::Vec3(2.0f, 0.0f, 0.0f)) == 1.0f); CPPUNIT_ASSERT(b1.getDistance(Geometry::Vec3(0.0f, -3.0f, 0.0f)) == 1.0f); CPPUNIT_ASSERT(b1.getDistance(Geometry::Vec3(0.0f, 3.0f, 0.0f)) == 1.0f); CPPUNIT_ASSERT(b1.getDistance(Geometry::Vec3(0.0f, 0.0f, -4.0f)) == 1.0f); CPPUNIT_ASSERT(b1.getDistance(Geometry::Vec3(0.0f, 0.0f, 4.0f)) == 1.0f); CPPUNIT_ASSERT(b1.getDistanceSquared(Geometry::Vec3(-2.0f, -3.0f, -4.0f)) == 3.0f); CPPUNIT_ASSERT(b1.getDistanceSquared(Geometry::Vec3(2.0f, 3.0f, 4.0f)) == 3.0f); // Make sure the box b1 was never changed. CPPUNIT_ASSERT(b1 == Geometry::Box(-1.0f, 1.0f, -2.0f, 2.0f, -3.0f, 3.0f)); }
void drawQuad(RenderingContext & rc, const Geometry::Vec3 & lowerLeft, const Geometry::Vec3 & lowerRight, const Geometry::Vec3 & upperRight, const Geometry::Vec3 & upperLeft) { static Util::Reference<Mesh> mesh; if (mesh.isNull()) { VertexDescription vertexDescription; vertexDescription.appendPosition3D(); vertexDescription.appendNormalFloat(); vertexDescription.appendTexCoord(); mesh = new Mesh(vertexDescription, 4, 6); MeshIndexData & id = mesh->openIndexData(); uint32_t * indices = id.data(); indices[0] = 0; indices[1] = 1; indices[2] = 2; indices[3] = 0; indices[4] = 2; indices[5] = 3; id.updateIndexRange(); id.markAsChanged(); } const Geometry::Vec3 edgeA = lowerRight - lowerLeft; const Geometry::Vec3 edgeB = upperLeft - lowerLeft; Geometry::Vec3 normal = edgeA.cross(edgeB); normal.normalize(); MeshVertexData & vd = mesh->openVertexData(); float * vertices = reinterpret_cast<float *> (vd.data()); // Lower left *vertices++ = lowerLeft.getX(); *vertices++ = lowerLeft.getY(); *vertices++ = lowerLeft.getZ(); *vertices++ = normal.getX(); *vertices++ = normal.getY(); *vertices++ = normal.getZ(); *vertices++ = 0.0f; *vertices++ = 0.0f; // Lower right *vertices++ = lowerRight.getX(); *vertices++ = lowerRight.getY(); *vertices++ = lowerRight.getZ(); *vertices++ = normal.getX(); *vertices++ = normal.getY(); *vertices++ = normal.getZ(); *vertices++ = 1.0f; *vertices++ = 0.0f; // Upper right *vertices++ = upperRight.getX(); *vertices++ = upperRight.getY(); *vertices++ = upperRight.getZ(); *vertices++ = normal.getX(); *vertices++ = normal.getY(); *vertices++ = normal.getZ(); *vertices++ = 1.0f; *vertices++ = 1.0f; // Upper left *vertices++ = upperLeft.getX(); *vertices++ = upperLeft.getY(); *vertices++ = upperLeft.getZ(); *vertices++ = normal.getX(); *vertices++ = normal.getY(); *vertices++ = normal.getZ(); *vertices++ = 0.0f; *vertices++ = 1.0f; vd.updateBoundingBox(); vd.markAsChanged(); rc.displayMesh(mesh.get()); }
btVector3 toBtVector3(const Geometry::Vec3& v){ return btVector3(v.x(),v.y(),v.z()); }