summaryrefslogtreecommitdiff
path: root/common/object.cpp
blob: 0d8c079885f46f9c1ec6cbe86410372a15c1cdb4 (plain)
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// Base class for all drawable objects
//

#include <stdlib.h>
#include <float.h>
#include <math.h>
#include "globals.h"
#include "project.h"
#include "object.h"
#include "matrix.h"
#include "vector.h"
#include "file.h"
#include "lc_application.h"

#define LC_KEY_SAVE_VERSION 1 // LeoCAD 0.73

// =============================================================================
// Static functions

// Returns in (A,B,C,D) the coefficientes of the plane with the three 
// succesive (in counterclockwise order) vertices p1,p2,p3. 
static void GetPolyCoeffs (float x1, float y1, float z1, float x2, float y2, float z2,
			   float x3, float y3, float z3, float *A, float *B, float *C, float *D)
{
  *A = ((y1-y2)*(z3-z2)) - ((z1-z2)*(y3-y2));
  *B = ((z1-z2)*(x3-x2)) - ((x1-x2)*(z3-z2)); 
  *C = ((x1-x2)*(y3-y2)) - ((y1-y2)*(x3-x2));
  *D = - ((*A)*x1) - ((*B)*y1) - ((*C)*z1);
}

// =============================================================================
// ClickLine structure

double LC_CLICKLINE::PointDistance (float *point)
{
  Vector op ((float)(point[0] - a1), (float)(point[1] - b1), (float)(point[2] - c1));
  Vector d ((float)a2, (float)b2, (float)c2);
  float len = d.Length ();
  d.Normalize ();
  float t = op.Dot (d);

  if (t > 0)
  {
    if (t >= len)
      t = 1;
    else
      t /= len;

    d *= (t*len);
    op -= d;
  }

  return op.Length ();
}

// =============================================================================
// Object class

Object::Object (LC_OBJECT_TYPE nType)
{
  //  m_nState = 0;
  //  m_strName[0] = '\0';

  m_pAnimationKeys = NULL;
  m_pInstructionKeys = NULL;

  m_nObjectType = nType;
  m_pKeyValues = NULL;

  //  m_pParent = NULL;
  //  m_pNext = NULL;
  //  m_pNextRender = NULL;
}

Object::~Object ()
{
  delete []m_pKeyValues;
  RemoveKeys ();
}

bool Object::FileLoad (File& file)
{
  lcuint8 version;

  file.ReadByte (&version, 1);
  if (version > LC_KEY_SAVE_VERSION)
    return false;

  lcuint16 time;
  float param[4];
  lcuint8 type;
  lcuint32 n;

  file.ReadLong (&n, 1);
  while (n--)
  {
    file.ReadShort (&time, 1);
    file.ReadFloat (param, 4);
    file.ReadByte (&type, 1);

    ChangeKey (time, false, true, param, type);
  }

  file.ReadLong (&n, 1);
  while (n--)
  {
    file.ReadShort (&time, 1);
    file.ReadFloat (param, 4);
    file.ReadByte (&type, 1);

    ChangeKey (time, true, true, param, type);
  }

  return true;
}

void Object::FileSave (File& file) const
{
  lcuint8 version = LC_KEY_SAVE_VERSION;
  LC_OBJECT_KEY *node;
  lcuint32 n;

  file.WriteByte (&version, 1);

  for (n = 0, node = m_pInstructionKeys; node; node = node->next)
    n++;
  file.WriteLong (&n, 1);

  for (node = m_pInstructionKeys; node; node = node->next)
  {
    file.WriteShort (&node->time, 1);
    file.WriteFloat (node->param, 4);
    file.WriteByte (&node->type, 1);
  }

  for (n = 0, node = m_pAnimationKeys; node; node = node->next)
    n++;
  file.WriteLong (&n, 1);

  for (node = m_pAnimationKeys; node; node = node->next)
  {
    file.WriteShort (&node->time, 1);
    file.WriteFloat (node->param, 4);
    file.WriteByte (&node->type, 1);
  }
}

// =============================================================================
// Key handling

static LC_OBJECT_KEY* AddNode (LC_OBJECT_KEY *node, unsigned short nTime, unsigned char nType)
{
  LC_OBJECT_KEY* newnode = (LC_OBJECT_KEY*)malloc (sizeof (LC_OBJECT_KEY));

  if (node)
  {
    newnode->next = node->next;
    node->next = newnode;
  }
  else
    newnode->next = NULL;

  newnode->type = nType;
  newnode->time = nTime;
  newnode->param[0] = newnode->param[1] = newnode->param[2] = newnode->param[3] = 0;

  return newnode;
}

void Object::RegisterKeys (float *values[], LC_OBJECT_KEY_INFO* info, int count)
{
  int i;

  m_pKeyValues = new float* [count];

  for (i = 0; i < count; i++)
    m_pKeyValues[i] = values[i];

  m_pAnimationKeys = AddNode (NULL, 1, 0);
  m_pInstructionKeys = AddNode (NULL, 1, 0);

  for (i = count-1; i > 0; i--)
  {
    AddNode (m_pAnimationKeys, 1, i);
    AddNode (m_pInstructionKeys, 1, i);
  }

  m_pKeyInfo = info;
  m_nKeyInfoCount = count;
}

void Object::RemoveKeys ()
{
  LC_OBJECT_KEY *node, *prev;

  for (node = m_pInstructionKeys; node;)
  {
    prev = node;
    node = node->next;
    free (prev);
  }

  for (node = m_pAnimationKeys; node;)
  {
    prev = node;
    node = node->next;
    free (prev);
  }
}

void Object::ChangeKey (unsigned short nTime, bool bAnimation, bool bAddKey, const float *param, unsigned char nKeyType)
{
  LC_OBJECT_KEY *node, *poskey = NULL, *newpos = NULL;
  if (bAnimation)
    node = m_pAnimationKeys;
  else
    node = m_pInstructionKeys;

  while (node)
  {
    if ((node->time <= nTime) &&
	(node->type == nKeyType))
      poskey = node;

    node = node->next;
  }

  if (bAddKey)
  {
    if (poskey)
    {
      if (poskey->time != nTime)
	newpos = AddNode(poskey, nTime, nKeyType);
    }
    else
      newpos = AddNode(poskey, nTime, nKeyType);
  }

  if (newpos == NULL)
    newpos = poskey;

  for (int i = 0; i < m_pKeyInfo[nKeyType].size; i++)
    newpos->param[i] = param[i];
}

void Object::CalculateKeys (unsigned short nTime, bool bAnimation)
{
//  LC_OBJECT_KEY *next[m_nKeyInfoCount], *prev[m_nKeyInfoCount], *node;
  LC_OBJECT_KEY *next[32], *prev[32], *node;
  int i, empty = m_nKeyInfoCount;

  for (i = 0; i < m_nKeyInfoCount; i++)
    next[i] = NULL;

  if (bAnimation)
    node = m_pAnimationKeys;
  else
    node = m_pInstructionKeys;

  // Get the previous and next keys for each variable
  while (node && empty)
  {
    if (node->time <= nTime)
    {
      prev[node->type] = node;
    }
    else
    {
      if (next[node->type] == NULL)
      {
        next[node->type] = node;
        empty--;
      }
    }

    node = node->next;
  }

  // TODO: USE KEY IN/OUT WEIGHTS
  for (i = 0; i < m_nKeyInfoCount; i++)
  {
    LC_OBJECT_KEY *n = next[i], *p = prev[i];

    if (bAnimation && (n != NULL) && (p->time != nTime))
    {
      float t = (float)(nTime - p->time)/(n->time - p->time);

      for (int j = 0; j < m_pKeyInfo[i].size; j++)
        m_pKeyValues[i][j] = p->param[j] + (n->param[j] - p->param[j])*t;
    }
    else
      for (int j = 0; j < m_pKeyInfo[i].size; j++)
        m_pKeyValues[i][j] = p->param[j];
  }
}

void Object::CalculateSingleKey (unsigned short nTime, bool bAnimation, int keytype, float *value) const
{
	LC_OBJECT_KEY *next = NULL, *prev = NULL, *node;

	if (bAnimation)
		node = m_pAnimationKeys;
	else
		node = m_pInstructionKeys;

	while (node)
	{
		if (node->type == keytype)
		{
			if (node->time <= nTime)
				prev = node;
			else
			{
				if (next == NULL)
				{
					next = node;
					break;
				}
			}
		}

		node = node->next;
	}

	// TODO: USE KEY IN/OUT WEIGHTS
	if (bAnimation && (next != NULL) && (prev->time != nTime))
	{
		float t = (float)(nTime - prev->time)/(next->time - prev->time);

		for (int j = 0; j < m_pKeyInfo[keytype].size; j++)
			value[j] = prev->param[j] + (next->param[j] - prev->param[j])*t;
	}
	else
		for (int j = 0; j < m_pKeyInfo[keytype].size; j++)
			value[j] = prev->param[j];
}

void Object::InsertTime (unsigned short start, bool animation, unsigned short time)
{
  LC_OBJECT_KEY *node, *prev = NULL;
  unsigned short last;
  bool end[32];
  int i;

  for (i = 0; i < m_nKeyInfoCount; i++)
    end[i] = false;

  if (animation)
  {
    node = m_pAnimationKeys;
    last = lcGetActiveProject()->GetTotalFrames ();
  }
  else
  {
    node = m_pInstructionKeys;
    last = 255;
  }

  for (; node != NULL; prev = node, node = node->next)
  {
    // skip everything before the start time
    if ((node->time < start) || (node->time == 1))
      continue;

    // there's already a key at the end, delete this one
    if (end[node->type])
    {
      prev->next = node->next;
      free (node);
      node = prev;

      continue;
    }

    node->time += time;
    if (node->time >= last)
    {
      node->time = last;
      end[node->type] = true;
    }
  }
}

void Object::RemoveTime (unsigned short start, bool animation, unsigned short time)
{
  LC_OBJECT_KEY *node, *prev = NULL;

  if (animation)
    node = m_pAnimationKeys;
  else
    node = m_pInstructionKeys;

  for (; node != NULL; prev = node, node = node->next)
  {
    // skip everything before the start time
    if ((node->time < start) || (node->time == 1))
      continue;

    if (node->time < (start + time))
    {
      // delete this key
      prev->next = node->next;
      free (node);
      node = prev;

      continue;
    }

    node->time -= time;
    if (node->time < 1)
      node->time = 1;
  }
}

// =============================================================================
// BoundingBox stuff

// Find the distance from the object to the beginning of the "click line".
double Object::BoundingBoxIntersectDist (LC_CLICKLINE* pLine) const
{
  double x, y, z;

  if (BoundingBoxIntersectionbyLine (pLine->a1, pLine->b1, pLine->c1, pLine->a2, pLine->b2, pLine->c2, &x, &y, &z))
    return (float)sqrt ((pLine->a1-x)*(pLine->a1-x)+(pLine->b1-y)*(pLine->b1-y)+(pLine->c1-z)*(pLine->c1-z));

  return DBL_MAX;
}

// Returns TRUE if the specified point is inside the bounding box of this object.
bool Object::BoundingBoxPointInside(double x, double y, double z) const
{
  int i = 0;
  while (i < 6 && ((m_fBoxPlanes[0][i]*x + m_fBoxPlanes[1][i]*y + 
		    m_fBoxPlanes[2][i]*z + m_fBoxPlanes[3][i]) <= 0.001)) 
    i++;
  return (i == 6);
}

// Returns TRUE if the line is intersecting any of the planes of the bounding
// box and if this point is also inside this bounding box.
bool Object::BoundingBoxIntersectionbyLine (double a1, double b1, double c1, double a2, double b2,
					    double c2, double *x, double *y, double *z) const
{
  double curr_t = DBL_MAX;
  double t, t1, t2;

  for (int i = 0; i < 6; i++)
  {
    t1 = (m_fBoxPlanes[0][i]*a1 + m_fBoxPlanes[1][i]*b1 + m_fBoxPlanes[2][i]*c1 + m_fBoxPlanes[3][i]);
    t2 = (m_fBoxPlanes[0][i]*a2 + m_fBoxPlanes[1][i]*b2 + m_fBoxPlanes[2][i]*c2);

    if (t1!=0 && t2!=0)
    {
      t = -( t1 / t2 );
      if (t>=0)
      {
	*x=a1+a2*t;
	*y=b1+b2*t;
	*z=c1+c2*t;

	if (BoundingBoxPointInside(*x,*y,*z))
	  if (t < curr_t)
	    curr_t = t;
      }
    }
  }

  if (curr_t != DBL_MAX)
  {
    *x=a1+a2*curr_t;
    *y=b1+b2*curr_t;
    *z=c1+c2*curr_t;
    return true;
  }
  else
    return false;
}

// For pieces
void Object::BoundingBoxCalculate (Matrix *mat, float Dimensions[6])
{
  //   BASE       TOP
  // 1------3  .------4  ^ X
  // |      |  |      |  |
  // |      |  |      |  |   Y
  // 0------.  2------5  .--->

  float pts[18] = {
    Dimensions[0], Dimensions[1], Dimensions[5],
    Dimensions[3], Dimensions[1], Dimensions[5],
    Dimensions[0], Dimensions[1], Dimensions[2],
    Dimensions[3], Dimensions[4], Dimensions[5],
    Dimensions[3], Dimensions[4], Dimensions[2],
    Dimensions[0], Dimensions[4], Dimensions[2] };

  mat->TransformPoints(pts, 6);

  GetPolyCoeffs (pts[3], pts[4], pts[5],  pts[0], pts[1], pts[2],  pts[6], pts[7], pts[8],
		 &m_fBoxPlanes[0][0], &m_fBoxPlanes[1][0], &m_fBoxPlanes[2][0], &m_fBoxPlanes[3][0]); //1,0,2
  GetPolyCoeffs (pts[9], pts[10],pts[11], pts[12],pts[13],pts[14], pts[15],pts[16],pts[17],
		 &m_fBoxPlanes[0][1], &m_fBoxPlanes[1][1], &m_fBoxPlanes[2][1], &m_fBoxPlanes[3][1]); //3,4,5
  GetPolyCoeffs (pts[15],pts[16],pts[17], pts[6], pts[7], pts[8],  pts[0], pts[1], pts[2],
		 &m_fBoxPlanes[0][2], &m_fBoxPlanes[1][2], &m_fBoxPlanes[2][2], &m_fBoxPlanes[3][2]); //5,2,0
  GetPolyCoeffs (pts[12],pts[13],pts[14], pts[9], pts[10],pts[11], pts[3], pts[4], pts[5],
		 &m_fBoxPlanes[0][3], &m_fBoxPlanes[1][3], &m_fBoxPlanes[2][3], &m_fBoxPlanes[3][3]); //4,3,1
  GetPolyCoeffs (pts[6], pts[7], pts[8],  pts[15],pts[16],pts[17], pts[12],pts[13],pts[14],
		 &m_fBoxPlanes[0][4], &m_fBoxPlanes[1][4], &m_fBoxPlanes[2][4], &m_fBoxPlanes[3][4]); //2,5,4
  GetPolyCoeffs (pts[0], pts[1], pts[2],  pts[3], pts[4], pts[5],  pts[9], pts[10],pts[11],
		 &m_fBoxPlanes[0][5], &m_fBoxPlanes[1][5], &m_fBoxPlanes[2][5], &m_fBoxPlanes[3][5]); //0,1,3
}

// Cameras
void Object::BoundingBoxCalculate (Matrix *mat)
{
  float normals[6][3] = {
    { 1,0,0 }, { 0,1,0 }, { 0,0,1 },
    { -1,0,0 }, { 0,-1,0 }, { 0,0,-1 } };
  float x,y,z,dist;

  if (IsCamera ())
    dist = 0.3f;
  else
    dist = 0.2f;

  mat->GetTranslation(&x,&y,&z);
  mat->SetTranslation(0,0,0);
  mat->TransformPoints(&normals[0][0], 6);

  for (int i = 0; i < 6; i++)
  {
    m_fBoxPlanes[0][i] = normals[i][0];
    m_fBoxPlanes[1][i] = normals[i][1];
    m_fBoxPlanes[2][i] = normals[i][2];

    float pt[3];
    pt[0] = dist*normals[i][0] + x;
    pt[1] = dist*normals[i][1] + y;
    pt[2] = dist*normals[i][2] + z;

    m_fBoxPlanes[3][i] = -(pt[0]*normals[i][0]+pt[1]*normals[i][1]+pt[2]*normals[i][2]);
  }
}

// Light
void Object::BoundingBoxCalculate (float pos[3])
{
  float pts[18] = {
     0.3f+pos[0],  0.3f+pos[1], -0.3f+pos[2],
    -0.3f+pos[0],  0.3f+pos[1], -0.3f+pos[2],
     0.3f+pos[0],  0.3f+pos[1],  0.3f+pos[2],
    -0.3f+pos[0], -0.3f+pos[1], -0.3f+pos[2],
    -0.3f+pos[0], -0.3f+pos[1],  0.3f+pos[2],
     0.3f+pos[0], -0.3f+pos[1],  0.3f+pos[2] };

  GetPolyCoeffs (pts[3], pts[4], pts[5],  pts[0], pts[1], pts[2],  pts[6], pts[7], pts[8],
		 &m_fBoxPlanes[0][0], &m_fBoxPlanes[1][0], &m_fBoxPlanes[2][0], &m_fBoxPlanes[3][0]); //1,0,2
  GetPolyCoeffs (pts[9], pts[10],pts[11], pts[12],pts[13],pts[14], pts[15],pts[16],pts[17],
		 &m_fBoxPlanes[0][1], &m_fBoxPlanes[1][1], &m_fBoxPlanes[2][1], &m_fBoxPlanes[3][1]); //3,4,5
  GetPolyCoeffs (pts[15],pts[16],pts[17], pts[6], pts[7], pts[8],  pts[0], pts[1], pts[2],
		 &m_fBoxPlanes[0][2], &m_fBoxPlanes[1][2], &m_fBoxPlanes[2][2], &m_fBoxPlanes[3][2]); //5,2,0
  GetPolyCoeffs (pts[12],pts[13],pts[14], pts[9], pts[10],pts[11], pts[3], pts[4], pts[5],
		 &m_fBoxPlanes[0][3], &m_fBoxPlanes[1][3], &m_fBoxPlanes[2][3], &m_fBoxPlanes[3][3]); //4,3,1
  GetPolyCoeffs (pts[6], pts[7], pts[8],  pts[15],pts[16],pts[17], pts[12],pts[13],pts[14],
		 &m_fBoxPlanes[0][4], &m_fBoxPlanes[1][4], &m_fBoxPlanes[2][4], &m_fBoxPlanes[3][4]); //2,5,4
  GetPolyCoeffs (pts[0], pts[1], pts[2],  pts[3], pts[4], pts[5],  pts[9], pts[10],pts[11],
		 &m_fBoxPlanes[0][5], &m_fBoxPlanes[1][5], &m_fBoxPlanes[2][5], &m_fBoxPlanes[3][5]); //0,1,3
}