EGDOF1.C
上传用户:tomyuan
上传日期:2019-01-23
资源大小:40306k
文件大小:8k
源码类别:

操作系统开发

开发平台:

C/C++

  1. /*******************************************************************************
  2.   Sample file: EGDOF1.C
  3.   Objective:Shows how to create DOF records.
  4. Shows how to set local origins and limits in DOF records.  
  5.   Program functions: Create new database with name from command line.
  6. Builds 3 bars, chain them together under DOF records.
  7. Set the local origin and pitch limits of each bar.
  8.   API functions used: mgSetAttList(), mgAttach(), mgNewRec(),
  9. mgSetColorIndex(), mgSetCurrentColorName(),
  10. mgAttach(), mgSetIcoord(), mgNewDb(), mgCloseDb()
  11. mgWriteDb().
  12.  ******************************************************************************/
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include "mgapiall.h"
  16.  /* defines */
  17. #define XDIR 1
  18. #define YDIR 2
  19. #define ZDIR 3
  20. #define NXDIR 4
  21. #define NYDIR 5
  22. #define NZDIR 6
  23. /* prototypes */
  24. void addVertex(mgrec *db, mgrec *prec, double x, double y, double z); 
  25. mgrec *makePoly(mgrec *db, unsigned int color, double offsetx, double offsety, double offsetz,
  26.  double size1, double size2, int direction);
  27. mgrec *makeMovingPart(mgrec *db, unsigned int color, double offsetx, double offsety, double offsetz);
  28. void main ( int argc, char* argv[] )
  29. {
  30. mgrec* db;
  31. mgrec *grec, *dofrec1, *dofrec2, *dofrec3, *orec;
  32. double doforgx=0., doforgy=0., doforgz=0.;
  33. char *siteid, *message;
  34. unsigned int red, green, blue, yellow;
  35. float inten;
  36. /* check for correct number of arguments */
  37. if (argc < 2) {
  38. printf ("Usage: %s file_namen", argv[0]);
  39. exit(1);
  40. }
  41. /* always call mgInit before any other API calls */
  42. mgInit ( &argc, argv );
  43. /* set red, green, blue, and yellow color indices */
  44. mgRGB2Index (db, (short)255, (short)0,  (short)0, &red, &inten);
  45. mgRGB2Index (db, (short)255, (short)255,  (short)0, &yellow, &inten);
  46. mgRGB2Index (db, (short)0, (short)255,  (short)0, &green, &inten);
  47. mgRGB2Index (db, (short)0, (short)0,  (short)255, &blue, &inten);
  48. /* start a new OpenFlight database, overwrite if exists */
  49. mgSetNewOverwriteFlag(mgTRUE);
  50. if (!(db = mgNewDb(argv[1]))) {
  51. mgGetError(&siteid, &message);
  52. printf("nError from %s: %sn", siteid, message);
  53. exit(1);
  54. }
  55. /* create group, 3 DOFs, chain DOFs under group */
  56. grec = mgNewRec(fltGroup);
  57. mgAttach(db, grec);
  58. dofrec1 = mgNewRec(fltDof);
  59. dofrec2 = mgNewRec(fltDof);
  60. dofrec3 = mgNewRec(fltDof);
  61. mgAttach (grec, dofrec1);
  62. mgAttach (dofrec1, dofrec2);
  63. mgAttach (dofrec2, dofrec3);
  64. /* make red bar under DOF 1, green under DOF 2, yellow under DOF 3 */
  65. orec = makeMovingPart(db, red, 50., 0., 0.);
  66. mgAttach(dofrec1, orec);
  67. orec = makeMovingPart(db, green, 150., 0., 0.);
  68. mgAttach(dofrec2, orec);
  69. orec = makeMovingPart(db, yellow, 250., 0., 0.);
  70. mgAttach(dofrec3, orec);
  71. /* set local origins and limits of DOFs */
  72. mgSetAttList (dofrec1, fltDofPutAnchorX, doforgx, 
  73. fltDofPutAnchorY, doforgy, 
  74. fltDofPutAnchorZ, doforgz, 
  75. fltDofPutTrackX, doforgx, 
  76. fltDofPutTrackY, doforgy+1, 
  77. fltDofPutTrackZ, doforgz, 
  78. fltDofPutAlignX, doforgx+1, 
  79. fltDofPutAlignY, doforgy, 
  80. fltDofPutAlignZ, doforgz,
  81. fltDofMinIncl, -30., 
  82. fltDofMaxIncl, 30.,
  83. fltDofCurIncl, 0., 
  84. fltDofIncrementIncl, 1.0, mgNULL);
  85. mgSetAttList (dofrec2, fltDofPutAnchorX, doforgx+100, 
  86. fltDofPutAnchorY, doforgy, 
  87. fltDofPutAnchorZ, doforgz, 
  88. fltDofPutTrackX, doforgx, 
  89. fltDofPutTrackY, doforgy+1, 
  90. fltDofPutTrackZ, doforgz, 
  91. fltDofPutAlignX, doforgx+101, 
  92. fltDofPutAlignY, doforgy, 
  93. fltDofPutAlignZ, doforgz,
  94. fltDofMinIncl, -30., 
  95. fltDofMaxIncl, 30.,
  96. fltDofCurIncl, 0., 
  97. fltDofIncrementIncl, 1.0, mgNULL);
  98. mgSetAttList (dofrec3, fltDofPutAnchorX, doforgx+200, 
  99. fltDofPutAnchorY, doforgy, 
  100. fltDofPutAnchorZ, doforgz, 
  101. fltDofPutTrackX, doforgx, 
  102. fltDofPutTrackY, doforgy+1, 
  103. fltDofPutTrackZ, doforgz, 
  104. fltDofPutAlignX, doforgx+101, 
  105. fltDofPutAlignY, doforgy, 
  106. fltDofPutAlignZ, doforgz,
  107. fltDofMinIncl, -30., 
  108. fltDofMaxIncl, 30.,
  109. fltDofCurIncl, 0., 
  110. fltDofIncrementIncl, 1.0, mgNULL);
  111. mgWriteDb(db);
  112. mgCloseDb ( db );
  113. }
  114. void addVertex(mgrec *db, mgrec *prec, double x, double y, double z) 
  115. /* add a vertex to a polygon */
  116. {
  117. mgrec *vrec;
  118. vrec = mgNewRec(fltVertex);
  119. mgAppend(prec, vrec);
  120. mgSetIcoord(vrec, fltIcoord, x, y, z);
  121. }
  122. mgrec *makePoly(mgrec *db, unsigned int color, double offsetx, double offsety, double offsetz,
  123.  double size1, double size2, int direction)
  124. /* creates a new polygon record with 4 vertices, */
  125. /* returns ptr to new polygon record */
  126. {
  127. mgrec* prec;
  128. double p1[3], p2[3], p3[3], p4[3];
  129. double move1, move2;
  130. move1 = size1/2.;
  131. move2 = size2/2.;
  132. /* set vertices based on offset, size, and direction */
  133. switch (direction) {
  134. case XDIR:
  135. p1[0] = offsetx; p1[1] = -move2+offsety; p1[2] = -move1+offsetz;
  136. p2[0] = offsetx; p2[1] = move2+offsety; p2[2] = -move1+offsetz;
  137. p3[0] = offsetx; p3[1] = move2+offsety; p3[2] = move1+offsetz;
  138. p4[0] = offsetx; p4[1] = -move2+offsety; p4[2] = move1+offsetz;
  139. break;
  140. case YDIR:
  141. p4[0] = -move1+offsetx; p4[1] = offsety; p4[2] = -move2+offsetz;
  142. p3[0] = move1+offsetx; p3[1] = offsety; p3[2] = -move2+offsetz;
  143. p2[0] = move1+offsetx; p2[1] = offsety; p2[2] = move2+offsetz;
  144. p1[0] = -move1+offsetx; p1[1] = offsety; p1[2] = move2+offsetz;
  145. break;
  146. case ZDIR:
  147. p1[0] = -move1+offsetx; p1[1] = -move2+offsety; p1[2] = offsetz;
  148. p2[0] = move1+offsetx; p2[1] = -move2+offsety; p2[2] = offsetz;
  149. p3[0] = move1+offsetx; p3[1] = move2+offsety; p3[2] = offsetz;
  150. p4[0] = -move1+offsetx; p4[1] = move2+offsety; p4[2] = offsetz;
  151. break;
  152. case NXDIR:
  153. p4[0] = offsetx; p4[1] = -move2+offsety; p4[2] = -move1+offsetz;
  154. p3[0] = offsetx; p3[1] = move2+offsety; p3[2] = -move1+offsetz;
  155. p2[0] = offsetx; p2[1] = move2+offsety; p2[2] = move1+offsetz;
  156. p1[0] = offsetx; p1[1] = -move2+offsety; p1[2] = move1+offsetz;
  157. break;
  158. case NYDIR:
  159. p1[0] = -move1+offsetx; p1[1] = offsety; p1[2] = -move2+offsetz;
  160. p2[0] = move1+offsetx; p2[1] = offsety; p2[2] = -move2+offsetz;
  161. p3[0] = move1+offsetx; p3[1] = offsety; p3[2] = move2+offsetz;
  162. p4[0] = -move1+offsetx; p4[1] = offsety; p4[2] = move2+offsetz;
  163. break;
  164. case NZDIR:
  165. p4[0] = -move1+offsetx; p4[1] = -move2+offsety; p4[2] = offsetz;
  166. p3[0] = move1+offsetx; p3[1] = -move2+offsety; p3[2] = offsetz;
  167. p2[0] = move1+offsetx; p2[1] = move2+offsety; p2[2] = offsetz;
  168. p1[0] = -move1+offsetx; p1[1] = move2+offsety; p1[2] = offsetz;
  169. break;
  170. }
  171. /* make polygon, attach to object */
  172. prec = mgNewRec(fltPolygon);
  173. /* make vertices, attach to polygon */
  174. addVertex(db, prec, p1[0], p1[1], p1[2]);
  175. addVertex(db, prec, p2[0], p2[1], p2[2]);
  176. addVertex(db, prec, p3[0], p3[1], p3[2]);
  177. addVertex(db, prec, p4[0], p4[1], p4[2]);
  178. /* set color */
  179. mgSetAttList(prec, fltPolyPrimeColor, color, mgNULL);
  180. return prec;
  181. }
  182. mgrec *makeMovingPart(mgrec *db, unsigned int color, double offsetx, double offsety, double offsetz)
  183. /* makes articulated part 100X10, centered at given offset */
  184. {
  185. mgrec *orec, *prec;
  186. double size1=100, size2=10;
  187. double move1, move2;
  188. move1 = size1/2.;
  189. move2 = size2/2.;
  190. orec = mgNewRec(fltObject);
  191. prec = makePoly(db, color, offsetx, offsety, offsetz+move2, size1, size2, ZDIR);
  192. mgAttach(orec, prec);
  193. prec = makePoly(db, color, offsetx, offsety+move2, offsetz, size1, size2, YDIR);
  194. mgAttach(orec, prec);
  195. prec = makePoly(db, color, offsetx, offsety, offsetz-move2, size1, size2, NZDIR);
  196. mgAttach(orec, prec);
  197. prec = makePoly(db, color, offsetx, offsety-move2, offsetz, size1, size2, NYDIR);
  198. mgAttach(orec, prec);
  199. prec = makePoly(db, color, offsetx+move1, offsety, offsetz, size2, size2, XDIR);
  200. mgAttach(orec, prec);
  201. prec = makePoly(db, color, offsetx-move1, offsety, offsetz, size2, size2, NXDIR);
  202. mgAttach(orec, prec);
  203. return orec;
  204. }