15 #include "arToolKit.h" 19 #include "pandaNode.h" 21 #include "perspectiveLens.h" 22 #include "lvecBase3.h" 23 #include "compose_matrix.h" 24 #include "config_vision.h" 29 ARToolKit::PatternTable ARToolKit::_pattern_table;
31 static void change_size( ARParam *source,
int xsize,
int ysize, ARParam *newparam )
35 newparam->xsize = xsize;
36 newparam->ysize = ysize;
38 double xscale = (double)xsize / (
double)(source->xsize);
39 double yscale = (double)ysize / (
double)(source->ysize);
40 for( i = 0; i < 4; i++ ) {
41 newparam->mat[0][i] = source->mat[0][i] * xscale;
42 newparam->mat[1][i] = source->mat[1][i] * yscale;
43 newparam->mat[2][i] = source->mat[2][i];
46 newparam->dist_factor[0] = source->dist_factor[0] * xscale;
47 newparam->dist_factor[1] = source->dist_factor[1] * yscale;
48 newparam->dist_factor[2] = source->dist_factor[2] / (xscale*yscale);
49 newparam->dist_factor[3] = source->dist_factor[3];
52 static void analyze_fov(
double cparam[3][4],
int width,
int height,
double &xfov,
double &yfov)
58 double p[3][3], q[4][4];
62 if( arParamDecompMat(cparam, icpara, trans) < 0 ) {
63 printf(
"gConvGLcpara: Parameter error!!\n");
67 for( i = 0; i < 3; i++ ) {
68 for( j = 0; j < 3; j++ ) {
69 p[i][j] = icpara[i][j] / icpara[2][2];
72 q[0][0] = (2.0 * p[0][0] / width);
73 q[0][1] = (2.0 * p[0][1] / width);
74 q[0][2] = ((2.0 * p[0][2] / width) - 1.0);
78 q[1][1] = (2.0 * p[1][1] / height);
79 q[1][2] = ((2.0 * p[1][2] / height) - 1.0);
84 q[2][2] = (gfar + gnear)/(gfar - gnear);
85 q[2][3] = -2.0 * gfar * gnear / (gfar - gnear);
93 q[0][0] * trans[0][0] +
94 q[0][1] * trans[1][0] +
95 q[0][2] * trans[2][0];
97 q[1][0] * trans[0][1] +
98 q[1][1] * trans[1][1] +
99 q[1][2] * trans[2][1];
101 xfov = 2.0 * atan(1.0/xval) * (180.0/3.141592654);
102 yfov = 2.0 * atan(1.0/yval) * (180.0/3.141592654);
119 ARToolKit *ARToolKit::
122 if (AR_DEFAULT_PIXEL_FORMAT != AR_PIXEL_FORMAT_BGRA &&
123 AR_DEFAULT_PIXEL_FORMAT != AR_PIXEL_FORMAT_RGBA &&
124 AR_DEFAULT_PIXEL_FORMAT != AR_PIXEL_FORMAT_ARGB &&
125 AR_DEFAULT_PIXEL_FORMAT != AR_PIXEL_FORMAT_ABGR &&
126 AR_DEFAULT_PIXEL_FORMAT != AR_PIXEL_FORMAT_RGB &&
127 AR_DEFAULT_PIXEL_FORMAT != AR_PIXEL_FORMAT_BGR) {
128 vision_cat.error() <<
129 "The copy of ARToolKit that you are using is not compiled " 130 "for RGB, BGR, RGBA, BGRA, ARGB or ABGR input. Panda3D cannot " 131 "use this copy of ARToolKit. Please modify the ARToolKit's " 132 "config file and compile it again.\n";
137 vision_cat.error() <<
"ARToolKit: invalid camera nodepath\n";
141 if ((node == 0) || (node->get_type() != Camera::get_class_type())) {
142 vision_cat.error() <<
"ARToolKit: invalid camera nodepath\n";
147 if (lens->get_type() != PerspectiveLens::get_class_type()) {
148 vision_cat.error() <<
"ARToolKit: supplied camera node must be perspective.\n";
154 if( arParamLoad(fn.c_str(), 1, &wparam) < 0 ) {
155 vision_cat.error() <<
"Cannot load ARToolKit camera config\n";
159 arParamDisp(&wparam);
161 analyze_fov(wparam.mat, 640, 480, xfov, yfov);
165 ARToolKit *result =
new ARToolKit();
166 result->_camera = camera;
167 result->_camera_param =
new ARParam;
168 result->_threshold = 0.5;
169 result->_marker_size = marker_size;
170 result->_have_prev_conv =
false;
171 memcpy(result->_camera_param, &wparam,
sizeof(wparam));
184 ARParam *param = (ARParam *)_camera_param;
196 ARToolKit() : _have_prev_conv(false) {
217 get_pattern(
const Filename &filename) {
218 PatternTable::iterator ptf = _pattern_table.find(filename);
219 if (ptf != _pattern_table.end()) {
220 return (*ptf).second;
224 int id = arLoadPatt(fn.c_str());
226 vision_cat.error() <<
"Could not load AR ToolKit Pattern: " << fn <<
"\n";
229 arDeactivatePatt(
id);
230 _pattern_table[filename] = id;
244 int patt = get_pattern(filename);
245 if (patt < 0)
return;
246 _controls[patt] = path;
271 analyze(
Texture *tex,
bool do_flip_texture) {
280 vision_cat.error() <<
"ARToolKit can only analyze RGB and RGBA textures.\n";
289 nassertv((xsize > 0) && (ysize > 0));
295 change_size((ARParam*)_camera_param, xsize, ysize, &cparam);
296 arInitCparam(&cparam);
304 const unsigned char *ram = ri.
p();
307 vision_cat.warning() <<
"No data in texture!\n";
312 unsigned char *dstrow;
313 const unsigned char *srcrow;
315 if (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_RGB ||
316 AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_BGR) {
317 data =
new unsigned char[xsize * ysize * 3];
318 int dstlen = xsize * 3;
320 if (do_flip_texture) {
321 for (
int y = 0; y < ysize; ++y) {
322 int invy = (ysize - y - 1);
323 memcpy(data + invy * dstlen, ram + y * srclen, dstlen);
325 }
else if (dstlen == srclen) {
326 memcpy(data, ram, ysize * srclen);
328 for (
int y = 0; y < ysize; ++y) {
329 memcpy(data + y * dstlen, ram + y * srclen, dstlen);
334 if (do_flip_texture) {
335 for (
int y = 0; y < ysize; ++y) {
336 dstrow = data + dstlen * (ysize - y - 1);
337 srcrow = ram + srclen * y;
338 for (
int x = 0; x < xsize; ++x) {
339 memcpy(dstrow + x * 3, srcrow + x * 4, 3);
343 for (
int y = 0; y < ysize; y++) {
344 dstrow = data + dstlen * y;
345 srcrow = ram + srclen * y;
346 for (
int x = 0; x < xsize; x++) {
347 memcpy(dstrow + x * 3, srcrow + x * 4, 3);
352 }
else if (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_RGBA ||
353 AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_BGRA) {
354 data =
new unsigned char[xsize * ysize * 4];
355 int dstlen = xsize * 4;
358 if (do_flip_texture) {
359 for (
int y = 0; y < ysize; ++y) {
360 dstrow = data + dstlen * (ysize - y - 1);
361 srcrow = ram + srclen * y;
362 for (
int x = 0; x < xsize; ++x) {
363 memcpy(dstrow + x * 4, srcrow + x * 3, 3);
364 dstrow[x * 4 + 3] = 255;
368 for (
int y = 0; y < ysize; ++y) {
369 dstrow = data + dstlen * y;
370 srcrow = ram + srclen * y;
371 for (
int x = 0; x < xsize; ++x) {
372 memcpy(dstrow + x * 4, srcrow + x * 3, 3);
373 dstrow[x * 4 + 3] = 255;
378 if (do_flip_texture) {
379 for (
int y = 0; y < ysize; ++y) {
380 int invy = (ysize - y - 1);
381 memcpy(data + invy * dstlen, ram + y * srclen, dstlen);
383 }
else if (dstlen == srclen) {
384 memcpy(data, ram, ysize * srclen);
386 for (
int y = 0; y < ysize; ++y) {
387 memcpy(data + y * dstlen, ram + y * srclen, dstlen);
392 data =
new unsigned char[xsize * ysize * 4];
393 int dstlen = xsize * 4;
396 if (do_flip_texture) {
397 for (
int y = 0; y < ysize; ++y) {
398 dstrow = data + dstlen * (ysize - y - 1);
399 srcrow = ram + srclen * y;
400 for (
int x = 0; x < xsize; ++x) {
401 memcpy(dstrow + x * 4 + 1, srcrow + x * 3, 3);
406 for (
int y = 0; y < ysize; ++y) {
407 dstrow = data + dstlen * y;
408 srcrow = ram + srclen * y;
409 for (
int x = 0; x < xsize; ++x) {
410 memcpy(dstrow + x * 4 + 1, srcrow + x * 3, 3);
416 if (do_flip_texture) {
417 for (
int y = 0; y < ysize; ++y) {
418 dstrow = data + dstlen * (ysize - y - 1);
419 srcrow = ram + srclen * y;
420 for (
int x = 0; x < xsize; ++x) {
421 memcpy(dstrow + x * 4 + 1, srcrow + x * 4, 3);
422 dstrow[x * 4] = srcrow[x * 4 + 3];
426 for (
int y = 0; y < ysize; ++y) {
427 dstrow = data + dstlen * y;
428 srcrow = ram + srclen * y;
429 for (
int x = 0; x < xsize; ++x) {
430 memcpy(dstrow + x * 4 + 1, srcrow + x * 4, 3);
431 dstrow[x * 4] = srcrow[x * 4 + 3];
439 Controls::const_iterator ctrli;
440 for (ctrli = _controls.begin(); ctrli != _controls.end(); ++ctrli) {
441 arActivatePatt((*ctrli).first);
444 ARMarkerInfo *marker_info;
447 if (arDetectMarker(data, _threshold * 256, &marker_info, &marker_num) < 0) {
448 vision_cat.error() <<
"ARToolKit detection error.\n";
453 for (ctrli = _controls.begin(); ctrli != _controls.end(); ++ctrli) {
455 int pattern = (*ctrli).first;
456 arDeactivatePatt(pattern);
459 for (
int i = 0; i < marker_num; ++i) {
460 if (marker_info[i].
id == pattern) {
461 if (marker_info[i].cf >= conf) {
462 conf = marker_info[i].cf;
468 ARMarkerInfo *inf = &marker_info[best];
472 if (_have_prev_conv) {
473 arGetTransMatCont(inf, _prev_conv, center, _marker_size, _prev_conv);
475 arGetTransMat(inf, center, _marker_size, _prev_conv);
476 _have_prev_conv =
true;
479 for (
int i = 0; i < 4; ++i) {
480 mat(i, 0) = _prev_conv[0][i];
481 mat(i, 1) = _prev_conv[2][i];
482 mat(i, 2) = -_prev_conv[1][i];
487 decompose_matrix(mat, scale, shear, hpr, pos);
490 vision_cat.error() <<
"NodePath must have a parent.\n";
504 #endif // HAVE_ARTOOLKIT CPTA_uchar get_ram_image()
Returns the system-RAM image data associated with the texture.
A basic node of the scene graph or data graph.
This is the base class for all three-component vectors and points.
int get_pad_y_size() const
Returns size of the pad region.
void set_pos_hpr(PN_stdfloat x, PN_stdfloat y, PN_stdfloat z, PN_stdfloat h, PN_stdfloat p, PN_stdfloat r)
Sets the translation and rotation component of the transform, leaving scale untouched.
A base class for any number of different kinds of lenses, linear and otherwise.
TextureType get_texture_type() const
Returns the overall interpretation of the texture.
bool is_empty() const
Returns true if the NodePath contains no nodes.
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
void show()
Undoes the effect of a previous hide() on this node: makes the referenced node (and the entire subgra...
ComponentType get_component_type() const
Returns the numeric interpretation of each component of the texture.
int get_pad_x_size() const
Returns size of the pad region.
Lens * get_lens(int index=0) const
Returns a pointer to the particular Lens associated with this LensNode, or NULL if there is not yet a...
The name of a file, such as a texture file or an Egg file.
This is a 4-by-4 transform matrix.
const Element * p() const
Function p() is similar to the function from ConstPointerTo.
void clear()
Sets this NodePath to the empty NodePath.
CompressionMode get_ram_image_compression() const
Returns the compression mode in which the ram image is already stored pre-compressed.
PandaNode * node() const
Returns the referenced node of the path.
int get_y_size() const
Returns the height of the texture image in texels.
NodePath get_parent(Thread *current_thread=Thread::get_current_thread()) const
Returns the NodePath to the parent of the referenced node: that is, this NodePath, shortened by one node.
int get_num_components() const
Returns the number of color components for each texel of the texture image.
A node that can be positioned around in the scene graph to represent a point of view for rendering a ...
void hide()
Makes the referenced node (and the entire subgraph below this node) invisible to all cameras...
string to_os_specific() const
Converts the filename from our generic Unix-like convention (forward slashes starting with the root a...
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
void set_fov(PN_stdfloat fov)
Sets the horizontal field of view of the lens without changing the aspect ratio.
int get_x_size() const
Returns the width of the texture image in texels.