1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
#pragma once
#include "ibrush.h"
#include "ipatch.h"
#include "iundo.h"
#include "math/Vector3.h"
#include "math/Plane3.h"
#include "render/TexCoord2f.h"
#include "math/AABB.h"
#include "scenelib.h"
namespace test
{
namespace algorithm
{
// Creates a cubic brush with dimensions 64x64x64 at the given origin
inline scene::INodePtr createCubicBrush(const scene::INodePtr& parent,
const Vector3& origin = Vector3(0,0,0),
const std::string& material = "_default")
{
UndoableCommand cmd("createBrush");
auto brushNode = GlobalBrushCreator().createBrush();
scene::addNodeToContainer(brushNode, parent);
auto& brush = *Node_getIBrush(brushNode);
auto translation = Matrix4::getTranslation(origin);
brush.addFace(Plane3(+1, 0, 0, 64).transform(translation));
brush.addFace(Plane3(-1, 0, 0, 64).transform(translation));
brush.addFace(Plane3(0, +1, 0, 64).transform(translation));
brush.addFace(Plane3(0, -1, 0, 64).transform(translation));
brush.addFace(Plane3(0, 0, +1, 64).transform(translation));
brush.addFace(Plane3(0, 0, -1, 64).transform(translation));
brush.setShader(material);
brush.evaluateBRep();
return brushNode;
}
inline scene::INodePtr createCuboidBrush(const scene::INodePtr& parent,
const AABB& bounds = AABB(Vector3(0, 0, 0), Vector3(64,256,128)),
const std::string& material = "_default")
{
UndoableCommand cmd("createBrush");
auto brushNode = GlobalBrushCreator().createBrush();
parent->addChildNode(brushNode);
auto& brush = *Node_getIBrush(brushNode);
auto translation = Matrix4::getTranslation(-bounds.getOrigin());
brush.addFace(Plane3(+1, 0, 0, bounds.getExtents().x()).transform(translation));
brush.addFace(Plane3(-1, 0, 0, bounds.getExtents().x()).transform(translation));
brush.addFace(Plane3(0, +1, 0, bounds.getExtents().y()).transform(translation));
brush.addFace(Plane3(0, -1, 0, bounds.getExtents().y()).transform(translation));
brush.addFace(Plane3(0, 0, +1, bounds.getExtents().z()).transform(translation));
brush.addFace(Plane3(0, 0, -1, bounds.getExtents().z()).transform(translation));
brush.setShader(material);
brush.evaluateBRep();
return brushNode;
}
inline IFace* findBrushFaceWithNormal(IBrush* brush, const Vector3& normal)
{
for (auto i = 0; i < brush->getNumFaces(); ++i)
{
auto& face = brush->getFace(i);
if (math::isNear(face.getPlane3().normal(), normal, 0.01))
{
return &face;
}
}
return nullptr;
}
inline bool faceHasVertex(const IFace* face, const std::function<bool(const WindingVertex&)>& predicate)
{
const auto& winding = face->getWinding();
for (const auto& vertex : winding)
{
if (predicate(vertex))
{
return true;
}
}
return false;
}
inline Vector2 getFaceCentroid(const IFace* face)
{
if (face->getWinding().empty()) return { 0, 0 };
Vector2 centroid = face->getWinding()[0].texcoord;
for (std::size_t i = 1; i < face->getWinding().size(); ++i)
{
centroid += face->getWinding()[i].texcoord;
}
centroid /= static_cast<Vector2::ElementType>(face->getWinding().size());
return centroid;
}
inline scene::INodePtr createPatchFromBounds(const scene::INodePtr& parent,
const AABB& bounds = AABB(Vector3(0, 0, 0), Vector3(64, 256, 128)),
const std::string& material = "_default")
{
auto patchNode = GlobalPatchModule().createPatch(patch::PatchDefType::Def2);
parent->addChildNode(patchNode);
auto patch = Node_getIPatch(patchNode);
patch->setDims(3, 3);
patch->setShader(material);
for (std::size_t col = 0; col < patch->getWidth(); ++col)
{
auto extents = bounds.getExtents();
for (std::size_t row = 0; row < patch->getHeight(); ++row)
{
patch->ctrlAt(row, col).vertex = bounds.getOrigin() - bounds.getExtents();
patch->ctrlAt(row, col).vertex.x() += 2 * col * extents.x();
patch->ctrlAt(row, col).vertex.y() += 2 * row * extents.y();
}
}
patch->controlPointsChanged();
return patchNode;
}
inline bool faceHasVertex(const IFace* face, const Vector3& expectedXYZ, const Vector2& expectedUV)
{
return algorithm::faceHasVertex(face, [&](const WindingVertex& vertex)
{
return math::isNear(vertex.vertex, expectedXYZ, 0.01) && math::isNear(vertex.texcoord, expectedUV, 0.01);
});
}
inline void foreachPatchVertex(const IPatch& patch, const std::function<void(const PatchControl&)>& functor)
{
for (std::size_t col = 0; col < patch.getWidth(); ++col)
{
for (std::size_t row = 0; row < patch.getHeight(); ++row)
{
functor(patch.ctrlAt(row, col));
}
}
}
inline bool patchHasVertex(const IPatch& patch, const Vector3& position)
{
for (std::size_t col = 0; col < patch.getWidth(); ++col)
{
for (std::size_t row = 0; row < patch.getHeight(); ++row)
{
if (math::isNear(patch.ctrlAt(row, col).vertex, position, 0.01))
{
return true;
}
}
}
return false;
}
inline bool patchHasVertices(const IPatch& patch, const std::vector<Vector3>& positions)
{
bool result = patch.getWidth() > 0 && patch.getHeight() > 0;
for (const auto& position : positions)
{
if (!patchHasVertex(patch, position))
{
result = false;
break;
}
}
return result;
}
inline AABB getTextureSpaceBounds(const IPatch& patch)
{
AABB bounds;
foreachPatchVertex(patch, [&](const PatchControl& control)
{
const auto& uv = control.texcoord;
bounds.includePoint({ uv.x(), uv.y(), 0 });
});
return bounds;
}
inline AABB getTextureSpaceBounds(const IFace& face)
{
AABB bounds;
for (const auto& vertex : face.getWinding())
{
bounds.includePoint({ vertex.texcoord.x(), vertex.texcoord.y(), 0 });
}
return bounds;
}
inline void expectVerticesHaveBeenFlipped(int axis, const IPatch& patch, const std::vector<Vector2>& oldTexcoords, const Vector2& flipCenter)
{
auto old = oldTexcoords.begin();
algorithm::foreachPatchVertex(patch, [&](const PatchControl& ctrl)
{
// Calculate the mirrored coordinate
auto expectedTexcoord = *(old++);
expectedTexcoord[axis] = 2 * flipCenter[axis] - expectedTexcoord[axis];
EXPECT_EQ(ctrl.texcoord.x(), expectedTexcoord.x()) << "Mirrored vertex should be at " << expectedTexcoord;
EXPECT_EQ(ctrl.texcoord.y(), expectedTexcoord.y()) << "Mirrored vertex should be at " << expectedTexcoord;
});
}
inline void foreachFace(IBrush& brush, const std::function<void(IFace&)>& functor)
{
for (int i = 0; i < brush.getNumFaces(); ++i)
{
functor(brush.getFace(i));
}
}
}
}
|