Oolite
Loading...
Searching...
No Matches
OOVoxel.m
Go to the documentation of this file.
1/*
2
3OOVoxel.m
4
5Oolite
6Copyright (C) 2004-2013 Giles C Williams and contributors
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21MA 02110-1301, USA.
22
23*/
24
25#import "OOMaths.h"
26
27// routines concerning octree voxels
28
29int checkFace(Vector p, GLfloat rd)
30{
31 int faces = 0;
32 if (p.x > rd)
33 faces |= CUBE_FACE_RIGHT; // right
34 if (p.x < -rd)
35 faces |= CUBE_FACE_LEFT; // left
36 if (p.y > rd)
37 faces |= CUBE_FACE_TOP; // above
38 if (p.y < -rd)
39 faces |= CUBE_FACE_BOTTOM; // below
40 if (p.z > rd)
41 faces |= CUBE_FACE_FRONT; // ahead
42 if (p.z < -rd)
43 faces |= CUBE_FACE_BACK; // behind
44 return faces;
45}
46
47int checkBevel(Vector p, GLfloat rd)
48{
49 GLfloat r2 = rd * 2;
50 int bevels = 0;
51 if (p.x + p.y > r2)
52 bevels |= 0x001;
53 if (p.x - p.y > r2)
54 bevels |= 0x002;
55 if (-p.x + p.y > r2)
56 bevels |= 0x004;
57 if (-p.x - p.y > r2)
58 bevels |= 0x008;
59 if (p.x + p.z > r2)
60 bevels |= 0x010;
61 if (p.x - p.z > r2)
62 bevels |= 0x020;
63 if (-p.x + p.z > r2)
64 bevels |= 0x040;
65 if (-p.x - p.z > r2)
66 bevels |= 0x080;
67 if (p.y + p.z > r2)
68 bevels |= 0x100;
69 if (p.y - p.z > r2)
70 bevels |= 0x200;
71 if (-p.y + p.z > r2)
72 bevels |= 0x400;
73 if (-p.y - p.z > r2)
74 bevels |= 0x800;
75 return bevels;
76}
77
78int checkCorner(Vector p, GLfloat rd)
79{
80 GLfloat r3 = rd * 3;
81 int corners = 0;
82 if ((p.x + p.y + p.z) > r3)
83 corners |= 0x01;
84 if ((p.x + p.y - p.z) > r3)
85 corners |= 0x02;
86 if ((p.x - p.y + p.z) > r3)
87 corners |= 0x04;
88 if ((p.x - p.y - p.z) > r3)
89 corners |= 0x08;
90 if ((-p.x + p.y + p.z) > r3)
91 corners |= 0x10;
92 if ((-p.x + p.y - p.z) > r3)
93 corners |= 0x20;
94 if ((-p.x - p.y + p.z) > r3)
95 corners |= 0x40;
96 if ((-p.x - p.y - p.z) > r3)
97 corners |= 0x80;
98 return corners;
99}
100
101Vector lineIntersectionWithFace(Vector p1, Vector p2, long mask, GLfloat rd)
102{
103 if (CUBE_FACE_RIGHT & mask)
104 return make_vector(rd,
105 p1.y + (p2.y - p1.y) * (rd - p1.x) / (p2.x - p1.x),
106 p1.z + (p2.z - p1.z) * (rd - p1.x) / (p2.x - p1.x));
107
108 if (CUBE_FACE_LEFT & mask)
109 return make_vector(-rd,
110 p1.y + (p2.y - p1.y) * (-rd - p1.x) / (p2.x - p1.x),
111 p1.z + (p2.z - p1.z) * (-rd - p1.x) / (p2.x - p1.x));
112
113 if (CUBE_FACE_TOP & mask)
114 return make_vector(p1.x + (p2.x - p1.x) * (rd - p1.y) / (p2.y - p1.y),
115 rd,
116 p1.z + (p2.z - p1.z) * (rd - p1.y) / (p2.y - p1.y));
117
118 if (CUBE_FACE_BOTTOM & mask)
119 return make_vector(p1.x + (p2.x - p1.x) * (-rd - p1.y) / (p2.y - p1.y),
120 -rd,
121 p1.z + (p2.z - p1.z) * (-rd - p1.y) / (p2.y - p1.y));
122
123 if (CUBE_FACE_FRONT & mask)
124 return make_vector(p1.x + (p2.x - p1.x) * (rd - p1.z) / (p2.z - p1.z),
125 p1.y + (p2.y - p1.y) * (rd - p1.z) / (p2.z - p1.z),
126 rd);
127
128 if (CUBE_FACE_BACK & mask)
129 return make_vector(p1.x + (p2.x - p1.x) * (-rd - p1.z) / (p2.z - p1.z),
130 p1.y + (p2.y - p1.y) * (-rd - p1.z) / (p2.z - p1.z),
131 -rd);
132 return p1;
133}
134
135int checkPoint(Vector p1, Vector p2, GLfloat alpha, long mask, GLfloat rd)
136{
137 Vector pp;
138 pp.x = p1.x + alpha * (p2.x - p1.x);
139 pp.y = p1.y + alpha * (p2.y - p1.y);
140 pp.z = p1.z + alpha * (p2.z - p1.z);
141 return (checkFace(pp, rd) & mask);
142}
143
144int checkLine(Vector p1, Vector p2, int mask, GLfloat rd)
145{
146 int result = 0;
147 if ((CUBE_FACE_RIGHT & mask) && (p1.x > p2.x) && (checkPoint(p1, p2, (rd - p1.x) / (p2.x - p1.x), 0x3f - CUBE_FACE_RIGHT, rd) == 0)) // right
148 result |= CUBE_FACE_RIGHT;
149 if ((CUBE_FACE_LEFT & mask) && (p1.x < p2.x) && (checkPoint(p1, p2, (-rd - p1.x) / (p2.x - p1.x), 0x3f - CUBE_FACE_LEFT, rd) == 0)) // left
150 result |= CUBE_FACE_LEFT;
151 if ((CUBE_FACE_TOP & mask) && (p1.y > p2.y) && (checkPoint(p1, p2, (rd - p1.y) / (p2.y - p1.y), 0x3f - CUBE_FACE_TOP, rd) == 0)) // above
152 result |= CUBE_FACE_TOP;
153 if ((CUBE_FACE_BOTTOM & mask) && (p1.y < p2.y) && (checkPoint(p1, p2, (-rd - p1.y) / (p2.y - p1.y), 0x3f - CUBE_FACE_BOTTOM, rd) == 0)) // below
154 result |= CUBE_FACE_BOTTOM;
155 if ((CUBE_FACE_FRONT & mask) && (p1.z > p2.z) && (checkPoint(p1, p2, (rd - p1.z) / (p2.z - p1.z), 0x3f - CUBE_FACE_FRONT, rd) == 0)) // ahead
156 result |= CUBE_FACE_FRONT;
157 if ((CUBE_FACE_BACK & mask) && (p1.z < p2.z) && (checkPoint(p1, p2, (-rd - p1.z) / (p2.z - p1.z), 0x3f - CUBE_FACE_BACK, rd) == 0)) // behind
158 result |= CUBE_FACE_BACK;
159 return result;
160}
161
162// line v0 to v1 is compared with a cube centered on the origin (corners at -rd,-rd,-rd to rd,rd,rd).
163// returns -1 if the line intersects the cube.
164int lineCubeIntersection(Vector v0, Vector v1, GLfloat rd)
165{
166 int v0_test, v1_test;
167
168 // compare both vertexes with all six face-planes
169 //
170 if ((v0_test = checkFace(v0, rd)) == 0)
171 return -1; // v0 is inside the cube
172 if ((v1_test = checkFace(v1, rd)) == 0)
173 return -1; // v1 is inside the cube
174
175 // check they're not both outside one face-plane
176 //
177 if ((v0_test & v1_test) != 0)
178 return 0; // both v0 and v1 are outside the same face of the cube
179
180 // Now do the same test for the 12 edge planes
181 //
182 v0_test |= checkBevel(v0, rd) << 8;
183 v1_test |= checkBevel(v1, rd) << 8;
184 if ((v0_test & v1_test) != 0)
185 return 0; // v0 and v1 outside of the same bevel
186
187 // Now do the same test for the 8 corner planes
188 //
189 v0_test |= checkCorner(v0, rd) << 24;
190 v1_test |= checkCorner(v1, rd) << 24;
191 if ((v0_test & v1_test) != 0)
192 return 0; // v0 and v1 outside of same corner
193
194 // see if the v0-->v1 line intersects the cube.
195 //
196 return checkLine(v0, v1, v0_test | v1_test, rd);
197}
OOINLINE Vector make_vector(OOScalar vx, OOScalar vy, OOScalar vz) INLINE_CONST_FUNC
Definition OOVector.h:149
#define CUBE_FACE_FRONT
Definition OOVoxel.h:33
#define CUBE_FACE_LEFT
Definition OOVoxel.h:30
#define CUBE_FACE_RIGHT
Definition OOVoxel.h:29
#define CUBE_FACE_TOP
Definition OOVoxel.h:31
#define CUBE_FACE_BOTTOM
Definition OOVoxel.h:32
#define CUBE_FACE_BACK
Definition OOVoxel.h:34
Vector lineIntersectionWithFace(Vector p1, Vector p2, long mask, GLfloat rd)
Definition OOVoxel.m:101
int checkCorner(Vector p, GLfloat rd)
Definition OOVoxel.m:78
int lineCubeIntersection(Vector v0, Vector v1, GLfloat rd)
Definition OOVoxel.m:164
int checkLine(Vector p1, Vector p2, int mask, GLfloat rd)
Definition OOVoxel.m:144
int checkPoint(Vector p1, Vector p2, GLfloat alpha, long mask, GLfloat rd)
Definition OOVoxel.m:135
int checkBevel(Vector p, GLfloat rd)
Definition OOVoxel.m:47
int checkFace(Vector p, GLfloat rd)
Definition OOVoxel.m:29
OOScalar x
Definition OOVector.h:32
OOScalar y
Definition OOVector.h:33
OOScalar z
Definition OOVector.h:34