/
Primitives.h
94 lines (71 loc) · 2.59 KB
/
Primitives.h
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
#pragma once
#include "ibrush.h"
#include "math/Plane3.h"
#include "math/AABB.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")
{
auto brushNode = GlobalBrushCreator().createBrush();
parent->addChildNode(brushNode);
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")
{
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; 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;
}
}
}