-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathShape.cpp
More file actions
158 lines (140 loc) · 3.83 KB
/
Shape.cpp
File metadata and controls
158 lines (140 loc) · 3.83 KB
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
#include "Shape.h"
#include "File.h"
#include <algorithm>
bool BoundingSphere::containsPoint(const Vector3& point) const
{
return (point - center).len3() <= radius;
}
bool BoundingSphere::intersectsWithSphere(const BoundingSphere& other) const
{
return (other.center - center).len3() <= radius + other.radius;
}
float BoundingSphere::distanceToPoint(const Vector3& point) const
{
return std::max(0.0f, (point - center).len3() - radius);
}
float BoundingSphere::distanceToSphere(const BoundingSphere& other) const
{
return std::max(0.0f, (other.center - center).len3() - radius - other.radius);
}
void BoundingSphere::merge(const BoundingSphere & other)
{
if (center == other.center) {
if (other.radius > radius)
radius = other.radius;
}
else {
Vector3 n_t2o = (other.center - center).normal();
Vector3 ext_t = center - n_t2o * radius;
Vector3 ext_o = other.center + n_t2o * other.radius;
if ((ext_o - center).len3() <= radius)
;
else if ((ext_t - other.center).len3() <= other.radius) {
center = other.center;
radius = other.radius;
}
else {
center = (ext_t + ext_o) * 0.5f;
radius = 0.5f * (ext_o - ext_t).len3();
}
}
}
void BoundingSphere::mergePoint(const Vector3 & point)
{
if ((point - center).len3() <= radius)
return;
Vector3 c2p = (point - center).normal();
Vector3 ext = center - c2p * radius;
center = (ext + point) * 0.5f;
radius = (point - center).len3();
}
void BoundingSphere::deserialize(File * file, bool withSquare)
{
for (float &c : center)
c = file->readFloat();
radius = file->readFloat();
if (withSquare)
file->readFloat();
}
void BoundingSphere::serialize(File * file, bool withSquare)
{
for (float &c : center)
file->writeFloat(c);
file->writeFloat(radius);
if (withSquare)
file->writeFloat(radius*radius);
}
void AABoundingBox::mergePoint(const Vector3 & point)
{
if (point.x > highCorner.x) highCorner.x = point.x;
if (point.y > highCorner.y) highCorner.y = point.y;
if (point.z > highCorner.z) highCorner.z = point.z;
if (point.x < lowCorner.x) lowCorner.x = point.x;
if (point.y < lowCorner.y) lowCorner.y = point.y;
if (point.z < lowCorner.z) lowCorner.z = point.z;
}
void AABoundingBox::merge(const AABoundingBox & box)
{
mergePoint(box.highCorner);
mergePoint(box.lowCorner);
}
bool AABoundingBox::containsPoint(const Vector3& point) const
{
return point.x >= lowCorner.x && point.x <= highCorner.x &&
point.y >= lowCorner.y && point.y <= highCorner.y &&
point.z >= lowCorner.z && point.z <= highCorner.z;
}
std::optional<AABoundingBox> AABoundingBox::intersectionWith(const AABoundingBox& box) const
{
AABoundingBox intersection;
for (int i = 0; i < 3; ++i) {
intersection.lowCorner.coord[i] = std::max(lowCorner.coord[i], box.lowCorner.coord[i]);
intersection.highCorner.coord[i] = std::min(highCorner.coord[i], box.highCorner.coord[i]);
if (intersection.lowCorner.coord[i] >= intersection.highCorner.coord[i]) {
return std::nullopt;
}
}
return intersection;
}
void AABoundingBox::deserialize(File * file)
{
for (float &f : highCorner)
f = file->readFloat();
for (float &f : lowCorner)
f = file->readFloat();
}
void AABoundingBox::serialize(File * file)
{
for (float &f : highCorner)
file->writeFloat(f);
for (float &f : lowCorner)
file->writeFloat(f);
}
void AABoundingBox::deserializeLC(File* file)
{
for (float& f : lowCorner)
f = file->readFloat();
for (float& f : highCorner)
f = file->readFloat();
}
void AABoundingBox::serializeLC(File* file)
{
for (float& f : lowCorner)
file->writeFloat(f);
for (float& f : highCorner)
file->writeFloat(f);
}
void AACylinder::deserialize(File * file)
{
for (float &f : center)
f = file->readFloat();
radius = file->readFloat();
height = file->readFloat();
}
void AACylinder::serialize(File * file)
{
for (float &f : center)
file->writeFloat(f);
file->writeFloat(radius);
file->writeFloat(height);
}