Triton
Visual simulation library for ocean rendering.
Vector3.h
Go to the documentation of this file.
1// Copyright (c) 2004-2012 Sundog Software, LLC All rights reserved worldwide.
2
7#ifdef SWIG
8%module TritonVector3
9#define TRITONAPI
10%{
11#include "Vector3.h"
12using namespace Triton;
13%}
14#endif
15
16#ifndef TRITON_VECTOR3_H
17#define TRITON_VECTOR3_H
18
19#if defined(__INTEL_COMPILER)
20#include <mathimf.h>
21#else
22#include <math.h>
23#endif
24
25#include "MemAlloc.h"
26#include <stdio.h>
27#include <iostream>
28
29#pragma pack(push)
30#pragma pack(8)
31
32namespace Triton
33{
35class Vector3 : public MemObject
36{
37public:
40 x = y = z = 0;
41 }
42
44 Vector3(double px, double py, double pz) : x(px), y(py), z(pz) {
45 }
46
48 Vector3(const double *p) : x(p[0]), y(p[1]), z(p[2]) {
49 }
50
52 Vector3(const Vector3& v) : x(v.x), y(v.y), z(v.z) {
53 }
54
56 double TRITONAPI Length() const {
57 return sqrt(x*x + y*y + z*z);
58 }
59
62 double TRITONAPI SquaredLength() const {
63 return (x*x + y*y + z*z);
64 }
65
67 void TRITONAPI Normalize() {
68 if (SquaredLength() > 0) {
69 double n = 1.0 / Length();
70 x *= n;
71 y *= n;
72 z *= n;
73 }
74 }
75
78 double TRITONAPI Dot (const Vector3& v) const {
79 return x * v.x + y * v.y + z * v.z;
80 }
81
84 Vector3 TRITONAPI Cross (const Vector3& v) const {
85 Vector3 kCross;
86
87 kCross.x = y*v.z - z*v.y;
88 kCross.y = z*v.x - x*v.z;
89 kCross.z = x*v.y - y*v.x;
90
91 return kCross;
92 }
93
95 Vector3 TRITONAPI operator * (double n) const {
96 return Vector3(x*n, y*n, z*n);
97 }
98
100 Vector3 TRITONAPI operator * (const Vector3& v) const {
101 return Vector3(x*v.x, y*v.y, z*v.z);
102 }
103
105 Vector3 TRITONAPI operator + (double n) const {
106 return Vector3(x+n, y+n, z+n);
107 }
108
110 Vector3 TRITONAPI operator - (const Vector3& v) const {
111 return Vector3(x - v.x, y - v.y, z- v.z);
112 }
113
115 Vector3 TRITONAPI operator + (const Vector3& v) const {
116 return Vector3(x + v.x, y + v.y, z + v.z);
117 }
118
120 bool TRITONAPI operator == (const Vector3& v) const {
121 return (v.x == x && v.y ==y && v.z == z);
122 }
123
125 bool TRITONAPI operator != (const Vector3& v) const {
126 return (v.x != x || v.y != y || v.z != z);
127 }
128
130 void TRITONAPI Serialize(std::ostream& s) const {
131 s.write((char *)&x, sizeof(double));
132 s.write((char *)&y, sizeof(double));
133 s.write((char *)&z, sizeof(double));
134 }
135
137 void TRITONAPI Unserialize(std::istream& s) {
138 s.read((char *)&x, sizeof(double));
139 s.read((char *)&y, sizeof(double));
140 s.read((char *)&z, sizeof(double));
141 }
142
144 double x, y, z;
145};
146
148class Vector3f : public MemObject
149{
150public:
153 }
154
156 Vector3f(const Triton::Vector3& v) : x((float)v.x), y((float)v.y), z((float)v.z) {
157 }
158
161 Vector3f(float px, float py, float pz) : x(px), y(py), z(pz) {
162 }
163
165 float TRITONAPI Length() {
166 return (float)sqrt(x*x + y*y + z*z);
167 }
168
170 void TRITONAPI Normalize() {
171 float n = 1.0f / Length();
172 x *= n;
173 y *= n;
174 z *= n;
175 }
176
178 double TRITONAPI Dot (const Vector3f& v) const {
179 return x * v.x + y * v.y + z * v.z;
180 }
181
183 Vector3f TRITONAPI operator - (const Vector3f& v) const {
184 return Vector3(x - v.x, y - v.y, z- v.z);
185 }
186
188 Vector3f TRITONAPI operator + (const Vector3f& v) const {
189 return Vector3(x + v.x, y + v.y, z + v.z);
190 }
191
193 Vector3f TRITONAPI operator * (const Vector3f& v) const {
194 return Vector3f(x*v.x, y*v.y, z*v.z);
195 }
196
198 Vector3f TRITONAPI operator * (float n) const {
199 return Vector3f(x*n, y*n, z*n);
200 }
201
203 float x, y, z;
204};
205
206
207//JWH
208class TBoundingBox : public MemObject
209{
210public:
211 TBoundingBox()
212 {
213 }
214
215 TBoundingBox(const Triton::Vector3& vMin, const Triton::Vector3& vMax)
216 {
217 m_vMin = vMin;
218 m_vMax = vMax;
219 }
220
221 TBoundingBox(double minX, double minY, double minZ, double maxX, double maxY, double maxZ)
222 {
223 m_vMin = Triton::Vector3(minX, minY, minZ);
224 m_vMax = Triton::Vector3(maxX, maxY, maxZ);
225 }
226
227 inline bool Contains (const Triton::Vector3& v) const
228 {
229 return ((v.x >= m_vMin.x) && (v.y >= m_vMin.y) && (v.z >= m_vMin.z) &&
230 (v.x <= m_vMax.x) && (v.y <= m_vMax.y) && (v.z <= m_vMax.z));
231 }
232
233 Vector3 m_vMin;
234 Vector3 m_vMax;
235};
236}
237
238#pragma pack(pop)
239
240#endif
Memory allocation interface for SilverLining.
A 3D Vector class and its operations.
This base class for all Triton objects intercepts the new and delete operators, routing them through ...
Definition: MemAlloc.h:71
A 3D double-precision Vector class and its operations.
Definition: Vector3.h:36
void TRITONAPI Normalize()
Scales the vector to be of length 1.0.
Definition: Vector3.h:67
Vector3(const Vector3 &v)
Copy constructor.
Definition: Vector3.h:52
double TRITONAPI Length() const
Returns the length of the vector.
Definition: Vector3.h:56
bool TRITONAPI operator==(const Vector3 &v) const
Tests if two vectors are exactly equal.
Definition: Vector3.h:120
Vector3(double px, double py, double pz)
Constructs a Vector3 with the given x,y,z coordinates.
Definition: Vector3.h:44
void TRITONAPI Unserialize(std::istream &s)
Restore this vector from a file.
Definition: Vector3.h:137
double TRITONAPI SquaredLength() const
Returns the squared length of the vector, which is faster than computing the length.
Definition: Vector3.h:62
double x
Data members x,y,z public for convenience.
Definition: Vector3.h:144
Vector3 TRITONAPI operator-(const Vector3 &v) const
Subtracts the specified vector from this vector, and returns the result.
Definition: Vector3.h:110
Vector3()
Default constructor, initializes to (0,0,0).
Definition: Vector3.h:39
double TRITONAPI Dot(const Vector3 &v) const
Determines the dot product between this vector and another, and returns the result.
Definition: Vector3.h:78
Vector3 TRITONAPI Cross(const Vector3 &v) const
Determines the cross product between this vector and another, and returns the result.
Definition: Vector3.h:84
Vector3 TRITONAPI operator+(double n) const
Adds a constant n to each component of the vector, and returns the result.
Definition: Vector3.h:105
Vector3 TRITONAPI operator*(double n) const
Scales each x,y,z value of the vector by a constant n, and returns the result.
Definition: Vector3.h:95
bool TRITONAPI operator!=(const Vector3 &v) const
Test if two vectors are not exactly equal.
Definition: Vector3.h:125
void TRITONAPI Serialize(std::ostream &s) const
Write this vector's data to a file.
Definition: Vector3.h:130
Vector3(const double *p)
Constructs a Vector 3 from a pointer to 3 doubles.
Definition: Vector3.h:48
A 3D single-precision vector class, and its operations.
Definition: Vector3.h:149
Vector3f(float px, float py, float pz)
Constructs a Vector3f from the specified single-precision floating point x, y, and z values.
Definition: Vector3.h:161
Vector3f TRITONAPI operator*(const Vector3f &v) const
Multiplies the components of two vectors together, and returns the result.
Definition: Vector3.h:193
Vector3f TRITONAPI operator+(const Vector3f &v) const
Adds this vector to the specified vector, and returns the result.
Definition: Vector3.h:188
Vector3f(const Triton::Vector3 &v)
Construct a single precision vector from a double precision one.
Definition: Vector3.h:156
void TRITONAPI Normalize()
Scales the vector to be of length 1.0.
Definition: Vector3.h:170
Vector3f TRITONAPI operator-(const Vector3f &v) const
Subtracts the specified vector from this vector, and returns the result.
Definition: Vector3.h:183
float x
Data members x, y, z are public for convenience.
Definition: Vector3.h:203
float TRITONAPI Length()
Returns the length of this vector.
Definition: Vector3.h:165
Vector3f()
Default constructor; does not initialize the vector.
Definition: Vector3.h:152
double TRITONAPI Dot(const Vector3f &v) const
Returns the dot product of this vector with the specified Vector3.
Definition: Vector3.h:178