Panda3D
linearNoiseForce.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file linearNoiseForce.cxx
10  * @author charles
11  * @date 2000-06-13
12  */
13 
14 #include <stdlib.h>
15 #include <math.h>
16 
17 #include "linearNoiseForce.h"
18 
19 // declare the statics
20 
21 ConfigVariableInt LinearNoiseForce::_random_seed
22 ("default_noise_force_seed", 665);
23 
24 bool LinearNoiseForce::_initialized = false;
25 unsigned char LinearNoiseForce::_prn_table[256];
26 LVector3 LinearNoiseForce::_gradient_table[256];
27 
28 TypeHandle LinearNoiseForce::_type_handle;
29 
30 /**
31  * One-time config function, sets up the PRN lattice.
32  */
35  // since this is a repeatable noise function, we always want to init with
36  // the same seed.
37  srand(_random_seed);
38 
39  LVector3 *gtable = _gradient_table;
40  for (int i = 0; i < 256; ++i) {
41  // fill the 1d array
42  _prn_table[i] = rand() & 255;
43 
44  // now fill the gradient array
45  *gtable++ = random_unit_vector();
46  }
47 }
48 
49 /**
50  * constructor
51  */
53 LinearNoiseForce(PN_stdfloat a, bool mass) :
54  LinearRandomForce(a, mass) {
55  if (_initialized == false) {
57  _initialized = true;
58  }
59 }
60 
61 /**
62  * copy constructor
63  */
66  LinearRandomForce(copy) {
67 }
68 
69 /**
70  * destructor
71  */
74 }
75 
76 /**
77  * copier
78  */
79 LinearForce *LinearNoiseForce::
80 make_copy() {
81  return new LinearNoiseForce(*this);
82 }
83 
84 /**
85  * Returns the noise value based on the object's position.
86  */
87 LVector3 LinearNoiseForce::
88 get_child_vector(const PhysicsObject *po) {
89  LPoint3 p = po->get_position();
90 
91  // get all of the components
92  int int_x, int_y, int_z;
93  PN_stdfloat frac_x, frac_y, frac_z;
94 
95  int_x = (int) p[0];
96  frac_x = p[0] - int_x;
97 
98  int_y = (int) p[1];
99  frac_y = p[1] - int_y;
100 
101  int_z = (int) p[2];
102  frac_z = p[2] - int_z;
103 
104  // apply the cubic smoother to the fractional values
105  PN_stdfloat cubic_x, cubic_y, cubic_z;
106 
107  cubic_x = cubic_step(frac_x);
108  cubic_y = cubic_step(frac_y);
109  cubic_z = cubic_step(frac_z);
110 
111  // trilinear interpolation into the cube (over, in, down)
112  LVector3 temp0, temp1, temp2, temp3;
113 
114  // x direction
115  temp0 = vlerp(cubic_x, get_lattice_entry(p),
116  get_lattice_entry(p[0] + 1.0f, p[1], p[2]));
117 
118  temp1 = vlerp(cubic_x, get_lattice_entry(p[0], p[1], p[2] + 1.0f),
119  get_lattice_entry(p[0] + 1.0f, p[1], p[2] + 1.0f));
120 
121  temp2 = vlerp(cubic_x, get_lattice_entry(p[0], p[1] + 1.0f, p[2]),
122  get_lattice_entry(p[0] + 1.0f, p[1] + 1.0f, p[2]));
123 
124  temp3 = vlerp(cubic_x, get_lattice_entry(p[0], p[1] + 1.0f, p[2] + 1.0f),
125  get_lattice_entry(p[0] + 1.0f, p[1] + 1.0f, p[2] + 1.0f));
126 
127  // y direction
128  temp0 = vlerp(cubic_z, temp0, temp1);
129  temp1 = vlerp(cubic_z, temp2, temp3);
130 
131  // z direction
132  return vlerp(cubic_y, temp0, temp1);
133 }
134 
135 /**
136  * Write a string representation of this instance to <out>.
137  */
139 output(std::ostream &out) const {
140  #ifndef NDEBUG //[
141  out<<""<<"LinearNoiseForce";
142  #endif //] NDEBUG
143 }
144 
145 /**
146  * Write a string representation of this instance to <out>.
147  */
149 write(std::ostream &out, int indent) const {
150  #ifndef NDEBUG //[
151  out.width(indent);
152  out<<""<<"LinearNoiseForce:";
153  out.width(indent+2); out<<""; out<<"_random_seed"<<_random_seed<<" (class)\n";
154  out.width(indent+2); out<<""; out<<"_initialized"<<_initialized<<" (class)\n";
155  out.width(indent+2); out<<""; out<<"_gradient_table"<<_gradient_table<<" (class)\n";
156  out.width(indent+2); out<<""; out<<"_prn_table"<<_prn_table<<" (class)\n";
158  #endif //] NDEBUG
159 }
virtual ~LinearNoiseForce()
destructor
Pure virtual, parent to noiseForce and jitterForce.
LinearNoiseForce(PN_stdfloat a=1.0f, bool m=false)
constructor
A body on which physics will be applied.
Definition: physicsObject.h:27
A force that acts on a PhysicsObject by way of an Integrator.
Definition: linearForce.h:23
virtual void write(std::ostream &out, int indent=0) const
Write a string representation of this instance to <out>.
virtual void output(std::ostream &out) const
Write a string representation of this instance to <out>.
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
Repeating noise force vector.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static void init_noise_tables()
One-time config function, sets up the PRN lattice.
LPoint3 get_position() const
Position Query.
This is a convenience class to specialize ConfigVariable as an integer type.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
virtual void write(std::ostream &out, int indent=0) const
Write a string representation of this instance to <out>.