author | Radek Brich <radek.brich@devl.cz> |
Wed, 23 Apr 2008 10:38:33 +0200 | |
branch | pyrit |
changeset 78 | 9569e9f35374 |
child 79 | 062b1c4143f7 |
permissions | -rw-r--r-- |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
1 |
/* |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
2 |
* shapes.h: shape classes |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
3 |
* |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
4 |
* This file is part of Pyrit Ray Tracer. |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
5 |
* |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
6 |
* Copyright 2006, 2007, 2008 Radek Brich |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
7 |
* |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
9 |
* of this software and associated documentation files (the "Software"), to deal |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
10 |
* in the Software without restriction, including without limitation the rights |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
11 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
12 |
* copies of the Software, and to permit persons to whom the Software is |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
13 |
* furnished to do so, subject to the following conditions: |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
14 |
* |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
15 |
* The above copyright notice and this permission notice shall be included in |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
16 |
* all copies or substantial portions of the Software. |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
17 |
* |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
21 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
23 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
24 |
* THE SOFTWARE. |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
25 |
*/ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
26 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
27 |
#ifndef SHAPES_H |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
28 |
#define SHAPES_H |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
29 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
30 |
#include "common.h" |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
31 |
#include "scene.h" |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
32 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
33 |
/* |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
34 |
triangle intersection alghoritm |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
35 |
options are: |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
36 |
TRI_PLUCKER |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
37 |
TRI_BARI |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
38 |
TRI_BARI_PRE |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
39 |
*/ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
40 |
#if !defined(TRI_PLUCKER) && !defined(TRI_BARI) && !defined(TRI_BARI_PRE) |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
41 |
# define TRI_BARI_PRE |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
42 |
#endif |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
43 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
44 |
/** |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
45 |
* shape |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
46 |
*/ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
47 |
class Shape |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
48 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
49 |
public: |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
50 |
Material *material; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
51 |
Shape() {}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
52 |
virtual ~Shape() {}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
53 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
54 |
// first intersection point |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
55 |
virtual bool intersect(const Ray &ray, Float &dist) const = 0; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
56 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
57 |
// all intersections (only for CSG) |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
58 |
virtual bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const = 0; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
59 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
60 |
// intersection with AABB |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
61 |
virtual bool intersect_bbox(const BBox &bbox) const = 0; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
62 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
63 |
// normal at point P |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
64 |
virtual const Vector3 normal(const Vector3 &P) const = 0; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
65 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
66 |
virtual BBox get_bbox() const = 0; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
67 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
68 |
virtual ostream & dump(ostream &st) const = 0; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
69 |
}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
70 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
71 |
/** |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
72 |
* list of shapes |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
73 |
*/ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
74 |
typedef vector<Shape*> ShapeList; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
75 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
76 |
/** |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
77 |
* sphere shape |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
78 |
*/ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
79 |
class Sphere: public Shape |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
80 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
81 |
Vector3 center; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
82 |
Float radius; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
83 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
84 |
Float sqr_radius; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
85 |
Float inv_radius; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
86 |
public: |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
87 |
Sphere(const Vector3 &acenter, const Float aradius, Material *amaterial): |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
88 |
center(acenter), radius(aradius), |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
89 |
sqr_radius(aradius*aradius), inv_radius(1.0f/aradius) |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
90 |
{ material = amaterial; } |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
91 |
bool intersect(const Ray &ray, Float &dist) const; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
92 |
bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
93 |
bool intersect_bbox(const BBox &bbox) const; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
94 |
const Vector3 normal(const Vector3 &P) const { return (P - center) * inv_radius; }; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
95 |
BBox get_bbox() const; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
96 |
const Vector3 getCenter() const { return center; }; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
97 |
const Float getRadius() const { return radius; }; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
98 |
ostream & dump(ostream &st) const; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
99 |
}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
100 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
101 |
/** |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
102 |
* box shape |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
103 |
*/ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
104 |
class Box: public Shape |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
105 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
106 |
Vector3 L; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
107 |
Vector3 H; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
108 |
public: |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
109 |
Box(const Vector3 &aL, const Vector3 &aH, Material *amaterial): L(aL), H(aH) |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
110 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
111 |
for (int i = 0; i < 3; i++) |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
112 |
if (L.cell[i] > H.cell[i]) |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
113 |
swap(L.cell[i], H.cell[i]); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
114 |
material = amaterial; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
115 |
}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
116 |
bool intersect(const Ray &ray, Float &dist) const; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
117 |
bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const { return false; }; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
118 |
bool intersect_bbox(const BBox &bbox) const; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
119 |
const Vector3 normal(const Vector3 &P) const; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
120 |
BBox get_bbox() const { return BBox(L, H); }; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
121 |
const Vector3 getL() const { return L; }; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
122 |
const Vector3 getH() const { return H; }; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
123 |
ostream & dump(ostream &st) const; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
124 |
}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
125 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
126 |
/** |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
127 |
* triangle vertex |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
128 |
*/ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
129 |
class Vertex |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
130 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
131 |
public: |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
132 |
Vector3 P; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
133 |
Vertex(const Vector3 &aP): P(aP) {}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
134 |
virtual ostream & dump(ostream &st) const; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
135 |
}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
136 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
137 |
/** |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
138 |
* triangle vertex with normal |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
139 |
*/ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
140 |
class NormalVertex: public Vertex |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
141 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
142 |
public: |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
143 |
Vector3 N; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
144 |
NormalVertex(const NormalVertex *v): Vertex(v->P), N(v->N) {}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
145 |
NormalVertex(const Vector3 &aP): Vertex(aP) {}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
146 |
NormalVertex(const Vector3 &aP, const Vector3 &aN): Vertex(aP), N(aN) {}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
147 |
const Vector3 &getNormal() { return N; }; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
148 |
void setNormal(const Vector3 &aN) { N = aN; }; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
149 |
ostream & dump(ostream &st) const; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
150 |
}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
151 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
152 |
/** |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
153 |
* triangle shape |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
154 |
*/ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
155 |
class Triangle: public Shape |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
156 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
157 |
#ifdef TRI_BARI_PRE |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
158 |
Float nu, nv, nd; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
159 |
int k; // dominant axis |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
160 |
Float bnu, bnv; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
161 |
Float cnu, cnv; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
162 |
#endif |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
163 |
#ifdef TRI_BARI |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
164 |
int k; // dominant axis |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
165 |
#endif |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
166 |
#ifdef TRI_PLUCKER |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
167 |
Float pla[6], plb[6], plc[6]; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
168 |
#endif |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
169 |
Vector3 N; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
170 |
const Vector3 smooth_normal(const Vector3 &P) const |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
171 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
172 |
#ifdef TRI_BARI_PRE |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
173 |
const Vector3 &NA = static_cast<NormalVertex*>(A)->N; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
174 |
const Vector3 &NB = static_cast<NormalVertex*>(B)->N; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
175 |
const Vector3 &NC = static_cast<NormalVertex*>(C)->N; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
176 |
static const int modulo3[5] = {0,1,2,0,1}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
177 |
register const int ku = modulo3[k+1]; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
178 |
register const int kv = modulo3[k+2]; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
179 |
const Float pu = P[ku] - A->P[ku]; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
180 |
const Float pv = P[kv] - A->P[kv]; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
181 |
const Float u = pv * bnu + pu * bnv; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
182 |
const Float v = pu * cnv + pv * cnu; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
183 |
Vector3 n = NA + u * (NB - NA) + v * (NC - NA); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
184 |
n.normalize(); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
185 |
return n; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
186 |
#else |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
187 |
return N; // not implemented for other algorithms |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
188 |
#endif |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
189 |
}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
190 |
public: |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
191 |
Vertex *A, *B, *C; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
192 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
193 |
Triangle() {}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
194 |
Triangle(Vertex *aA, Vertex *aB, Vertex *aC, Material *amaterial); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
195 |
bool intersect(const Ray &ray, Float &dist) const; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
196 |
bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const {return false;}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
197 |
bool intersect_bbox(const BBox &bbox) const; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
198 |
const Vector3 normal(const Vector3 &P) const { return (material->smooth ? smooth_normal(P) : N); }; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
199 |
const Vector3 getNormal() const { return N; }; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
200 |
BBox get_bbox() const; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
201 |
ostream & dump(ostream &st) const; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
202 |
}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
203 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
204 |
template <class T> class Array |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
205 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
206 |
T *array; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
207 |
public: |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
208 |
Array(int n) { array = new T[n]; }; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
209 |
~Array() { delete[] array; }; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
210 |
const T &operator[](int i) const { return array[i]; }; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
211 |
}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
212 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
213 |
typedef Array<Vertex> VertexArray; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
214 |
typedef Array<NormalVertex> NormalVertexArray; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
215 |
typedef Array<Triangle> TriangleArray; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
216 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
217 |
#endif |