File: sample.cpp

package info (click to toggle)
blender 2.42a-7
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 60,700 kB
  • ctags: 83,393
  • sloc: ansic: 576,763; cpp: 187,760; python: 48,472; sh: 15,785; makefile: 4,298; perl: 2,082; asm: 1,471; java: 8
file content (222 lines) | stat: -rwxr-xr-x 5,950 bytes parent folder | download | duplicates (5)
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
/*
 * SOLID - Software Library for Interference Detection
 * 
 * Copyright (C) 2001-2003  Dtecta.  All rights reserved.
 *
 * This library may be distributed under the terms of the Q Public License
 * (QPL) as defined by Trolltech AS of Norway and appearing in the file
 * LICENSE.QPL included in the packaging of this file.
 *
 * This library may be distributed and/or modified under the terms of the
 * GNU General Public License (GPL) version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * This library is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Commercial use or any other use of this library not covered by either 
 * the QPL or the GPL requires an additional license from Dtecta. 
 * Please contact info@dtecta.com for enquiries about the terms of commercial
 * use of this library.
 */

#include <stdio.h>

//#define USE_QUADS     //Use quad mesh instead of a triangle mesh
//#define USE_HULL      //Use the convex hulls of the rings 

#include <SOLID.h>

#include "MT_Point3.h"
#include "MT_Vector3.h"
#include "MT_Quaternion.h"

const MT_Scalar SPACE_SIZE = 20.0;

const int NUM_ITER   = 10000;
const int NUM_OBJECTS = 2;


typedef struct MyObject {
    int id;
	MT_Point3 pos;
    DT_ObjectHandle objectHandle;
} MyObject; 


/* ARGSUSED */
DT_Bool collide1(void * client_data, void *obj1, void *obj2,
			  const DT_CollData *coll_data)
{
	return DT_CONTINUE;
}

/* ARGSUSED */
DT_Bool collide2(void * client_data, void *obj1, void *obj2,
			  const DT_CollData *coll_data)
{
    FILE *stream = (FILE *)client_data;

    fprintf(stream, "Aha!, Object %d interferes with object %d\n", 
            (*(MyObject *)obj1).id, (*(MyObject *)obj2).id);

	return DT_CONTINUE;
}

#ifdef STATISTICS
extern int num_box_tests;
#endif

int main() 
{
	MyObject objects[NUM_OBJECTS];

	int i;
	for (i = 0; i != NUM_OBJECTS; ++i) 
	{
        objects[i].id = i;
		objects[i].pos.setValue(MT_random() * SPACE_SIZE, MT_random() * SPACE_SIZE, MT_random() * SPACE_SIZE);
	}

#ifdef USE_HULL
    DT_ShapeHandle shape = DT_NewPolytope(0);
#else
    DT_ShapeHandle shape = DT_NewComplexShape(0);
#endif

    MT_Scalar a = 10; 
    MT_Scalar b = 2; 

    fprintf(stderr, "Loading a torus with a major radius of %d and a minor radius of %d,\n", (int)a, (int)b); 

    const int n1 = 50;
    const int n2 = 50;

#ifdef USE_QUADS
    fprintf(stderr, "composed of %d quads...",n1 * n2); fflush(stderr); 
#else
    fprintf(stderr, "composed of %d triangles...", 2 * n1 * n2); fflush(stderr); 
#endif
    int uc;
    for (uc = 0; uc < n1; uc++) 
	{
        int vc;
        for (vc = 0; vc < n2; vc++)
		{
            MT_Scalar u1 = (MT_2_PI * uc) / n1; 
            MT_Scalar u2 = (MT_2_PI * (uc+1)) / n1; 
            MT_Scalar v1 = (MT_2_PI * vc) / n2; 
            MT_Scalar v2 = (MT_2_PI * (vc+1)) / n2; 
            
            MT_Scalar p1[3], p2[3], p3[3], p4[3];
            
            p1[0] = (a - b * MT_cos(v1)) * MT_cos(u1);
            p2[0] = (a - b * MT_cos(v1)) * MT_cos(u2);
            p3[0] = (a - b * MT_cos(v2)) * MT_cos(u1);
            p4[0] = (a - b * MT_cos(v2)) * MT_cos(u2);
            p1[1] = (a - b * MT_cos(v1)) * MT_sin(u1);
            p2[1] = (a - b * MT_cos(v1)) * MT_sin(u2);
            p3[1] = (a - b * MT_cos(v2)) * MT_sin(u1);
            p4[1] = (a - b * MT_cos(v2)) * MT_sin(u2);
            p1[2] = b * MT_sin(v1);
            p2[2] = b * MT_sin(v1);
            p3[2] = b * MT_sin(v2);
            p4[2] = b * MT_sin(v2);
            
#ifdef USE_QUADS

			DT_Begin();   
            DT_Vertex(p1);
            DT_Vertex(p2);
            DT_Vertex(p4);
            DT_Vertex(p3);
            DT_End();

#else      
            DT_Begin();
            DT_Vertex(p1);
            DT_Vertex(p2);
            DT_Vertex(p3);
            DT_End();

            DT_Begin();
            DT_Vertex(p4);
            DT_Vertex(p1);
            DT_Vertex(p2);
            DT_End();
#endif  
        }
    }
    fprintf(stderr, "done.\n");

#ifdef USE_HULL
    fprintf(stderr, "Building convex hull..."); fflush(stderr);
    DT_EndPolytope();
#else
	fprintf(stderr, "Building hierarchy..."); fflush(stderr);
    DT_EndComplexShape();
#endif

	fprintf(stderr, "done.\n");

    DT_SceneHandle scene = DT_CreateScene();
	
	DT_RespTableHandle respTable = DT_CreateRespTable();
	DT_ResponseClass responseClass = DT_GenResponseClass(respTable);
	
	for (i = 0; i != NUM_OBJECTS; ++i) 
	{
		objects[i].objectHandle = DT_CreateObject(&objects[i], shape);
		DT_AddObject(scene, objects[i].objectHandle); 
		DT_SetResponseClass(respTable, objects[i].objectHandle, responseClass);
	}
   
    DT_AddDefaultResponse(respTable, &collide1, DT_SIMPLE_RESPONSE, stdout);

	int col_count = 0;
    
    printf("Running %d tests on %d objects at random placements\n", NUM_ITER, NUM_OBJECTS);
    printf("in a space of size %.0f...\n", SPACE_SIZE);  
    int k;
	for (k = 0; k != NUM_ITER; ++k) 
	{
		for (i = 0; i != NUM_OBJECTS; ++i) 
		{ 
			objects[i].pos.setValue(MT_random() * SPACE_SIZE, 
									MT_random() * SPACE_SIZE, 
									MT_random() * SPACE_SIZE);

			DT_SetPosition(objects[i].objectHandle, objects[i].pos);
			DT_SetOrientation(objects[i].objectHandle, MT_Quaternion::random());
		}
       

        col_count += DT_Test(scene, respTable);
		//fprintf(stderr, "."); fflush(stderr);
    }
    printf("done\n");
    
    std::cout << "Number of collisions: " << col_count << std::endl;
#ifdef STATISTICS
    std::cout << "Number of sep_axis calls: " << num_box_tests << std::endl;
#endif
	for (i = 0; i != NUM_OBJECTS; ++i)
	{
		DT_RemoveObject(scene, objects[i].objectHandle);
		DT_DestroyObject(objects[i].objectHandle);
	}
	    
    DT_DestroyRespTable(respTable);
	DT_DestroyScene(scene);
    DT_DeleteShape(shape);
    
    return 0;
}