-
Notifications
You must be signed in to change notification settings - Fork 34
/
IAgentTracker.java
386 lines (326 loc) · 12.8 KB
/
IAgentTracker.java
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*---
iGeo - http://igeo.jp
Copyright (c) 2002-2013 Satoru Sugihara
This file is part of iGeo.
iGeo is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, version 3.
iGeo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with iGeo. If not, see <http://www.gnu.org/licenses/>.
---*/
package igeo;
import java.lang.reflect.Array;
/**
dynamic behavior for a geometry object to track particle location.
this class needs to be added under point agent (IPointAgent class) or its subclasses like IParticle and IBoid.
@author Satoru Sugihara
*/
public class IAgentTracker extends IDynamicsBase{
/** set agent's attributes to geometry objects */
static public boolean setAgentAttributes=true;
static public IVec defaultOrientation = new IVec(0,1,0);
public IGeometry[] geometries;
public IPointAgent agent;
/** if agent is particle, it's casted to particle variable */
public IParticleI particle;
public IVec prevPos;
public IVec prevVel;
public IVec rotAxis;
public double rotAngle=0;
public boolean orient=true;
/*
public IAgentTracker(IGeometry... geometries){
this.geometries = geometries;
}
*/
public IAgentTracker(IPointAgent p, IGeometry... geometries){
this.geometries = geometries;
initTracker(p);
}
/*
public IAgentTracker(IVecI geometryOrigin, IGeometry... geometries){
this.geometries = geometries;
}
*/
public IAgentTracker(IPointAgent p, IVecI geometryOrigin, IGeometry[] geometries){
this.geometries = geometries;
initTracker(geometryOrigin, p);
}
public IAgentTracker(IPointAgent p, IVecI geometryOrigin, IVecI geometryOrientation, IGeometry[] geometries){
this.geometries = geometries;
initTracker(geometryOrigin, geometryOrientation, p);
}
/** total number of geometries */
public int geometryNum(){ return geometries.length; }
/** returns first geomery */
public IGeometry geometry(){ if(geometries.length==0){ return null; } return geometries[0]; }
/** returns n-th geometry */
public IGeometry geometry(int i){ return geometries[i]; }
/** returns all geometries */
public IGeometry[] geometries(){ return geometries; }
/** returns all contained geometries in a specified type */
@SuppressWarnings({"unchecked"})
public <T extends IGeometry> T[] subgeometries(Class<T> cls){
//T[] geo = new T[subgeometryNum<T>()];
T[] geo = (T[])Array.newInstance(cls, subgeometryNum(cls));
for(int i=0, idx=0; i<geometries.length; i++)
if(cls.isInstance(geometries[i])) geo[idx++] = (T)geometries[i];
return geo;
}
/** returns first geometry in a specified type */
@SuppressWarnings({"unchecked"})
public <T extends IGeometry> T subgeometry(Class<T> cls){
for(int i=0; i<geometries.length; i++) if(cls.isInstance(geometries[i])) return (T)geometries[i];
return null; // not found
}
/** returns n-th geometry in a specified type */
@SuppressWarnings({"unchecked"})
public <T extends IGeometry> T subgeometry(Class<T> cls, int index){
for(int i=0, idx=0; i<geometries.length; i++){
if(cls.isInstance(geometries[i])){
if(idx == index) return (T)geometries[i];
idx++;
}
}
return null; // not found
}
/** returns number of geometries in a specified type */
public int subgeometryNum(Class<? extends IGeometry> cls){
int num=0;
for(int i=0; i<geometries.length; i++) if(cls.isInstance(geometries[i])) num++;
return num;
}
/** returns all contained points */
public IPoint[] points(){ return this.<IPoint>subgeometries(IPoint.class); }
/** returns first point */
public IPoint point(){ return this.<IPoint>subgeometry(IPoint.class); }
/** returns n-th point */
public IPoint point(int index){ return this.<IPoint>subgeometry(IPoint.class, index); }
/** returns point num */
public int pointNum(){ return this.subgeometryNum(IPoint.class); }
/** returns all contained curves */
public ICurve[] curves(){ return this.<ICurve>subgeometries(ICurve.class); }
/** returns first curve */
public ICurve curve(){ return this.<ICurve>subgeometry(ICurve.class); }
/** returns n-th curve */
public ICurve curve(int index){ return this.<ICurve>subgeometry(ICurve.class, index); }
/** returns curve num */
public int curveNum(){ return this.subgeometryNum(ICurve.class); }
/** returns all contained surfaces */
public ISurface[] surfaces(){ return this.<ISurface>subgeometries(ISurface.class); }
/** returns first surface */
public ISurface surface(){ return this.<ISurface>subgeometry(ISurface.class); }
/** returns n-th surface */
public ISurface surface(int index){ return this.<ISurface>subgeometry(ISurface.class, index); }
/** returns surface num */
public int surfaceNum(){ return this.subgeometryNum(ISurface.class); }
/** returns all contained breps */
public IBrep[] breps(){ return this.<IBrep>subgeometries(IBrep.class); }
/** returns first brep */
public IBrep brep(){ return this.<IBrep>subgeometry(IBrep.class); }
/** returns n-th surface */
public IBrep brep(int index){ return this.<IBrep>subgeometry(IBrep.class, index); }
/** returns brep num */
public int brepNum(){ return this.subgeometryNum(IBrep.class); }
/** returns all contained meshes */
public IMesh[] meshes(){ return this.<IMesh>subgeometries(IMesh.class); }
/** returns first mesh */
public IMesh mesh(){ return this.<IMesh>subgeometry(IMesh.class); }
/** returns n-th surface */
public IMesh mesh(int index){ return this.<IMesh>subgeometry(IMesh.class, index); }
/** returns mesh num */
public int meshNum(){ return this.subgeometryNum(IMesh.class); }
public IAgentTracker show(){
for(int i=0; i<geometries.length; i++) geometries[i].show();
return this;
}
public IAgentTracker hide(){
for(int i=0; i<geometries.length; i++) geometries[i].hide();
return this;
}
/** delete all geometries */
public void del(){
for(int i=0; i<geometries.length; i++) geometries[i].del(); // is this ok?
geometries = new IGeometry[0]; // to be safe
parent.deleteDynamics(this);
}
public IAgentTracker orient(boolean f){
if(particle!=null){ orient = f; }
else{ IOut.err("parent is not particle. orient is changed."); }
return this;
}
public IAgentTracker clr(IColor c){
for(int i=0; i<geometries.length; i++) geometries[i].clr(c);
return this;
}
public IAgentTracker clr(IColor c, int alpha){
for(int i=0; i<geometries.length; i++) geometries[i].clr(c,alpha);
return this;
}
public IAgentTracker clr(IColor c, float alpha){
for(int i=0; i<geometries.length; i++) geometries[i].clr(c,alpha);
return this;
}
public IAgentTracker clr(IColor c, double alpha){
for(int i=0; i<geometries.length; i++) geometries[i].clr(c,alpha);
return this;
}
public IAgentTracker clr(IObject o){
for(int i=0; i<geometries.length; i++) geometries[i].clr(o);
return this;
}
//public IAgentTracker clr(Color c){ super.clr(c); point.clr(c); return this; }
//public IAgentTracker clr(Color c, int alpha){ super.clr(c,alpha); point.clr(c,alpha); return this; }
//public IAgentTracker clr(Color c, float alpha){ super.clr(c,alpha); point.clr(c,alpha); return this; }
//public IAgentTracker clr(Color c, double alpha){ super.clr(c,alpha); point.clr(c,alpha); return this; }
public IAgentTracker clr(int gray){
for(int i=0; i<geometries.length; i++) geometries[i].clr(gray);
return this;
}
public IAgentTracker clr(float fgray){
for(int i=0; i<geometries.length; i++) geometries[i].clr(fgray);
return this;
}
public IAgentTracker clr(double dgray){
for(int i=0; i<geometries.length; i++) geometries[i].clr(dgray);
return this;
}
public IAgentTracker clr(int gray, int alpha){
for(int i=0; i<geometries.length; i++) geometries[i].clr(gray,alpha);
return this;
}
public IAgentTracker clr(float fgray, float falpha){
for(int i=0; i<geometries.length; i++) geometries[i].clr(fgray,falpha);
return this;
}
public IAgentTracker clr(double dgray, double dalpha){
for(int i=0; i<geometries.length; i++) geometries[i].clr(dgray,dalpha);
return this;
}
public IAgentTracker clr(int r, int g, int b){
for(int i=0; i<geometries.length; i++) geometries[i].clr(r,g,b);
return this;
}
public IAgentTracker clr(float fr, float fg, float fb){
for(int i=0; i<geometries.length; i++) geometries[i].clr(fr,fg,fb);
return this;
}
public IAgentTracker clr(double dr, double dg, double db){
for(int i=0; i<geometries.length; i++) geometries[i].clr(dr,dg,db);
return this;
}
public IAgentTracker clr(int r, int g, int b, int a){
for(int i=0; i<geometries.length; i++) geometries[i].clr(r,g,b,a);
return this;
}
public IAgentTracker clr(float fr, float fg, float fb, float fa){
for(int i=0; i<geometries.length; i++) geometries[i].clr(fr,fg,fb,fa);
return this;
}
public IAgentTracker clr(double dr, double dg, double db, double da){
for(int i=0; i<geometries.length; i++) geometries[i].clr(dr,dg,db,da);
return this;
}
public IAgentTracker hsb(float h, float s, float b, float a){
for(int i=0; i<geometries.length; i++) geometries[i].hsb(h,s,b,a);
return this;
}
public IAgentTracker hsb(double h, double s, double b, double a){
for(int i=0; i<geometries.length; i++) geometries[i].hsb(h,s,b,a);
return this;
}
public IAgentTracker hsb(float h, float s, float b){
for(int i=0; i<geometries.length; i++) geometries[i].hsb(h,s,b);
return this;
}
public IAgentTracker hsb(double h, double s, double b){
for(int i=0; i<geometries.length; i++) geometries[i].hsb(h,s,b);
return this;
}
/** move geometries from its center to the current position of the parent agent */
public void initTracker(IPointAgent p){
IVec center = new IVec();
for(int i=0; i<geometries.length; i++) center.add(geometries[i].center());
center.div(geometries.length);
initTracker(center, p);
}
public void initTracker(IVecI geometryOrigin, IPointAgent p){
initTracker(geometryOrigin, defaultOrientation, p);
}
public void initTracker(IVecI geometryOrigin, IVecI geometryOrientation, IPointAgent p){
agent = p;
agent.addDynamics(this);
if(agent instanceof IParticleI){ particle = (IParticleI)agent; }
else{ orient=false; }
if(geometryOrientation==null){ orient=false; }
if(setAgentAttributes){
if(agent.attr()!=null){
for(int i=0; i<geometries.length; i++){
if(geometries[i].attr()!=null){ geometries[i].attr().merge(agent.attr()); }
else{ geometries[i].attr(agent.attr()); }
}
}
else if(agent.clr()!=null){
if(agent.clr()!=null){
for(int i=0; i<geometries.length; i++) geometries[i].clr(agent.clr());
}
}
}
IVec dif = agent.pos().dif(geometryOrigin);
boolean modified=false;
if(dif.len()>0){
for(int i=0; i<geometries.length; i++){ geometries[i].add(dif); }
modified=true;
}
prevPos = agent.pos().dup();
if( particle!=null && geometryOrientation!=null ){
prevVel = particle.vel().dup();
if(prevVel.len()==0){ prevVel = geometryOrientation.get(); }
else if(geometryOrientation.len()>0){
rotAngle = geometryOrientation.angle(prevVel);
rotAxis = geometryOrientation.get().cross(prevVel);
if(rotAngle!=0){
for(int i=0; i<geometries.length; i++){
geometries[i].rot(agent.pos(),rotAxis,rotAngle);
}
modified=true;
}
}
}
if(modified){
for(int i=0; i<geometries.length; i++){ geometries[i].updateGraphic(); }
}
}
public void update(){
prevPos.sub(agent.pos()).neg(); // setting difference
if(orient && particle!=null){
if(particle.vel().len()==0) rotAngle=0;
//if(particle.vel().len()<IConfig.tolrance) rotAngle=0;
else{
rotAngle = prevVel.angle(particle.vel());
rotAxis = prevVel.cross(particle.vel());
prevVel = particle.vel().dup(); // already updated
if(rotAxis.len()==0){ rotAngle=0; }
}
}
boolean modified=false;
if(prevPos.len()>0){
for(int i=0; i<geometries.length; i++){ geometries[i].add(prevPos); }
modified=true;
}
if(orient && rotAngle!=0){
for(int i=0; i<geometries.length; i++){
if(orient && rotAngle!=0){ geometries[i].rot(agent.pos(),rotAxis,rotAngle); }
}
modified=true;
}
if(modified){
for(int i=0; i<geometries.length; i++){ geometries[i].updateGraphic(); }
}
prevPos.set(agent.pos());
}
}