home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD1.iso / GFX / Raytracing / Raytracer / LW5VT09.LHA / Toaster / Arexx_examples / PlugIns / CalcPosition.lwm < prev    next >
Encoding:
Text File  |  1996-06-10  |  13.0 KB  |  384 lines

  1. /* CMD: ½CalcPosition                                                   */
  2. /* By Brett Evan Hester      13032 Copenhill Rd. Dallas, Tx. 75240-5302 */
  3.     MacrosName = "CalcPosition"
  4. /* Macro Type:                                                          */
  5. /* CALCULATES  * OBJECTS   * SELECTED     * REMEMBERS                   */
  6. /* Description:                                                         */
  7.  
  8. Info1A = "!Calculate Position ©          Information 1 of 2"
  9. Info1B = ""
  10. Info1C = "@This macro will provide a position given an old "
  11. Info1D = "@position OR a direction and distance to head.   "
  12. Info1E = ""
  13. Info1F = "þ When calculating or using the HEADING & PITCH:"
  14. Info1G = "¤ Clockwise rotation is positive (+).           "
  15. Info1H = "¤ POINT A is considered the source.             "
  16. Info1I = "¤ POINT B is considered the target.             "
  17. Info1J = "¤ DISTANCE is direct from POINT A to POINT B.   "
  18. Info1K = ""
  19. Info1L = "@þ On the main menu:                             "
  20. Info1M = "OK will Update the Menu Requester and Continue. "
  21. Info1N = "CANCEL will Leave or Quit this macro.           "
  22.  
  23. Info2A = "!Calculate Position ©          Information 2 of 2"
  24. Info2B = "@               Plug-Ins and Go! ©               "
  25. Info2C = "                           Hester and associates"
  26. Info2D = "                           13032 Copenhill Road "
  27. Info2E = "                           Dallas, Texas 75240  "
  28. Info2F = "@Special Thanks to:                              "
  29. Info2G = "Arnie Cachelin  Henry Ribron    Mark J. Holland "
  30. Info2H = "J. Phil Kelso   Terry Wester    Steven K. Simms "
  31. Info2I = "Kevin DeRita    Greg Glaser     William S. Hawes"
  32. Info2J = "NewTek ©        Commodore ©     INOVAtronics ©  "
  33. Info2K = ""
  34. Info2L = "@This macro represents a lot of time & hard work."
  35. Info2M = "@Encourage people to create new ones and not kill"
  36. Info2N = "@that possibility by stealing those that are out."
  37.  
  38. /* -------------------------------------------------------------------- */
  39.                                      /* Start Error Detection (See End) */
  40. SIGNAL ON ERROR
  41. SIGNAL ON SYNTAX
  42.                                                    /* Address LightWave */
  43. VT3DLib = ADDLIB("LWModelerARexx.port",0)
  44. ADDRESS "LWModelerARexx.port"
  45.                                                   /* Add Math Functions */
  46. MATHLIB= "rexxmathlib.library"
  47. IF POS(MATHLIB , SHOW('L')) = 0 THEN
  48.     IF ~ADDLIB(MATHLIB , 0 , -30 , 0) THEN DO
  49.         CALL Notify(1,"!Can't find "MATHLIB)
  50.         IF VT3DLib THEN CALL REMLIB("LWModelerARexx.port")
  51.         EXIT
  52.     END
  53.  
  54. /* -------------------------------------------------------------------- */
  55.                                     /* Reading Global Macro Preferences */
  56. BEHDefaultFilePath = "Sys:"
  57. BEHSettingsSavedTo = "T:"
  58. BEHSpeechAndSound = "1"
  59.  
  60. IF (EXISTS("S:PlugInPrefs")) THEN DO
  61.     IF (~OPEN(PlugInPrefs, "S:PlugInPrefs", 'R')) THEN BREAK
  62.     IF (READLN(PlugInPrefs) ~= "PlugInPrefs") THEN BREAK
  63.     BEHDefaultFilePath = READLN(PlugInPrefs)
  64.     BEHSettingsSavedTo = READLN(PlugInPrefs)
  65.     BEHSpeechAndSound = READLN(PlugInPrefs)
  66.     CALL CLOSE PlugInPrefs
  67. END
  68.  
  69. /* -------------------------------------------------------------------- */
  70.                                             /* Recalling Macro Settings */
  71. XA = 0 ; YA = 0 ; ZA = 0
  72. XB = 0 ; YB = 0 ; ZB = 0
  73. ReqHeading = 0 ; ReqPitch = 0 ; ReqDistance = 0
  74. ReqCalc = 3
  75. ReqCreate = 1
  76.  
  77. PrefsFileName = BEHSettingsSavedTo||MacrosName||".PLUG"
  78.  
  79. IF (EXISTS(PrefsFileName)) THEN DO
  80.     IF (~OPEN(PrefsFile, PrefsFileName, 'R')) THEN BREAK
  81.     IF (READLN(PrefsFile) ~= MacrosName) THEN BREAK
  82.  
  83.     PARSE VALUE READLN(PrefsFile) WITH XA YA ZA
  84.     PARSE VALUE READLN(PrefsFile) WITH XB YB ZB
  85.     PARSE VALUE READLN(PrefsFile) WITH ReqHeading ReqPitch ReqDistance
  86.     ReqCalc = READLN(PrefsFile)
  87.     BEHInfoReqCreate = READLN(PrefsFile)
  88.  
  89.     CALL CLOSE PrefsFile
  90. END
  91.  
  92. /* -------------------------------------------------------------------- */
  93.                                              /* Setting fixed variables */
  94. PI = 3.141592653589793238462643
  95. RadToDeg = 180 / PI ; DegToRad = PI / 180
  96. QuarterTurn = PI / 2   /* 90°  1.570 Rad */
  97. HalfTurn = PI          /* 180° 3.142 Rad */
  98. FullTurn = PI * 2      /* 360° 6.283 Rad */
  99.  
  100. /* -------------------------------------------------------------------- */
  101.                                        /* Settings if 2 Points Selected */
  102. CALL Sel_Mode(USER)
  103.  
  104. NumberOfPnts = XFrm_Begin()
  105.  
  106. IF NumberOfPnts = 2 THEN DO
  107.     PARSE VALUE XFrm_GetPos(1) WITH XA YA ZA
  108.     PARSE VALUE XFrm_GetPos(2) WITH XB YB ZB
  109.     CALL XFrm_End
  110.     CALL CalcHPD
  111.     ReqCreate = 2
  112. END
  113.  
  114. /* ******************************************************************** */
  115.                                                    /* Main Body Of Code */
  116. DO FOREVER
  117.     CALL MenuRequester
  118.     IF ReqCalc ~= 3 THEN CALL CalcXYZ
  119.     IF ReqCalc = 3 THEN CALL CalcHPD
  120.     IF ReqCreate = 1 THEN CALL CreatePoints
  121. END
  122.  
  123. /* ******************************************************************** */
  124.                                                       /* User Interface */
  125. MenuRequester:
  126.  
  127.     CALL Req_Begin("Calculate Position ©                ")
  128.  
  129.     CALL Req_AddControl(" ",'T',"                   by Brett Hester ","")
  130.     CALL Req_AddControl("þ",'T',"Coords.   X          Y         Z")
  131.     ReqA = Req_AddControl("A",'V',1)
  132.     ReqB = Req_AddControl("B",'V',1)
  133.     CALL Req_AddControl(" ",'T',"")
  134.     CALL Req_AddControl("þ",'T',"Heading     Pitch       Distance")
  135.     ReqC = Req_AddControl("¤",'V',0)
  136.     CALL Req_AddControl(" ",'T',"")
  137.     CALL Req_AddControl("þ",'T',"Calculate")
  138.     ReqD = Req_AddControl("¤",'CH', " Point A   Point B   HP & D  ")
  139.     CALL Req_AddControl(" ",'T',"")
  140.     ReqE = Req_AddControl(" ",'CH', "Create Points X")
  141.     ReqF = Req_AddControl("¤",'CH', "Information")
  142.  
  143.     CALL Req_SetVal(ReqA, XA YA ZA)
  144.     CALL Req_SetVal(ReqB, XB YB ZB)
  145.     CALL Req_SetVal(ReqC, ReqHeading ReqPitch ReqDistance)
  146.     CALL Req_SetVal(ReqD, ReqCalc)
  147.     CALL Req_SetVal(ReqE, ReqCreate)
  148.     CALL Req_SetVal(ReqF, 0)
  149.  
  150.     IF Req_Post() = 0 THEN DO
  151.         CALL SaveSettings
  152.         CALL Exiting
  153.     END
  154.  
  155.     PARSE VALUE Req_GetVal(ReqA) WITH XA YA ZA
  156.     PARSE VALUE Req_GetVal(ReqB) WITH XB YB ZB
  157.     PARSE VALUE Req_GetVal(ReqC) WITH ReqHeading ReqPitch ReqDistance
  158.     ReqCalc = Req_GetVal(ReqD)
  159.     ReqCreate = Req_GetVal(ReqE)
  160.     BEHInfo = Req_GetVal(ReqF)
  161.  
  162.     CALL Req_End()
  163.  
  164.     ReqDistance = ABS(ReqDistance)
  165.  
  166.     IF BEHInfo = 0 THEN CALL CheckInput
  167.     IF BEHInfo = 1 THEN CALL InformationWindows
  168.  
  169. RETURN
  170.  
  171. /* -------------------------------------------------------------------- */
  172.                                                         /* Verify Input */
  173. CheckInput:
  174.  
  175.     IF ReqCalc ~= 3 THEN DO
  176.         IF ReqDistance = 0 THEN DO
  177.             CALL Notify(1,"!No Distance was provided.")
  178.             CALL MenuRequester
  179.         END
  180.     END
  181.  
  182.     OKtoContinue = 0
  183.     DO UNTIL OKtoContinue = 1
  184.         IF ReqPitch < -90 THEN DO
  185.             ReqPitch = -90 - (ReqPitch + 90)
  186.             IF ReqPitch > -270 THEN ReqHeading = ReqHeading - 180
  187.         END
  188.         IF ReqPitch > 90 THEN DO
  189.             ReqPitch = 90 - (ReqPitch - 90)
  190.             IF ReqPitch < 270 THEN ReqHeading = ReqHeading + 180
  191.         END
  192.         IF ReqPitch < 90 THEN DO
  193.             IF ReqPitch > -90 THEN OKtoContinue = 1
  194.         END
  195.     END
  196.  
  197.     OKtoContinue = 0
  198.     DO UNTIL OKtoContinue = 1
  199.         IF ReqHeading < -180 THEN ReqHeading = 180 + (ReqHeading +180)
  200.         IF ReqHeading > 180 THEN ReqHeading = -180 + (ReqHeading -180)
  201.         IF ReqHeading < 180 THEN DO
  202.             IF ReqHeading > -180 THEN OKtoContinue = 1
  203.         END
  204.     END
  205.  
  206. RETURN
  207.  
  208. /* -------------------------------------------------------------------- */
  209.                                    /* Calculating Quadrant of 2nd Point */
  210. CalcXYZ:
  211.     /******************************************************/
  212.     /*Quadrants:                                          */
  213.     /*       _______+Z_______                             */
  214.     /*      /  +IV   |  +I   \                            */
  215.     /*     /   -++   |  +++   \                           */
  216.     /* -X /----------+---------\ +X  Quad. XYZ P Heading  */
  217.     /*   /    +III   |   +II    \   ======================*/
  218.     /*  /     -+-    |   ++-     \   +I    +++ - + 0-90   */
  219.     /* /-------------+------------\  +II   ++- - + 90-180 */
  220.     /* |    |       -Z       |    |  +III  -+- - - 90-180 */
  221.     /* |    |                |    |  +IV   -++ - - 0-90   */
  222.     /* |    |_______+Z_______|    |  -I    +-+ + + 0-90   */
  223.     /* |    /  -IV   |  -I   \    |  -II   +-- + + 90-180 */
  224.     /*     /   --+   |  +-+   \      -III  --- + - 90-180 */
  225.     /* -X /----------+---------\ +X  -IV   --+ + - 0-90   */
  226.     /*   /    -III   |   -II    \                         */
  227.     /* |/     ---    |   +--     \|                       */
  228.     /* /-------------+------------\                       */
  229.     /*              -Z                                    */
  230.     /******************************************************/
  231.  
  232.     IF ReqPitch > 0 THEN YDiff = "-"  /* + Quadrant   */
  233.     IF ReqPitch <= 0 THEN YDiff = "+" /* - Quadrant   */
  234.  
  235.     IF ReqHeading >= 0 THEN DO
  236.         IF ReqHeading <= 90 THEN DO    /* Quadrant I   */
  237.             XDiff = "+" ; ZDiff = "+"
  238.         END
  239.         IF ReqHeading > 90 THEN DO     /* Quadrant II  */
  240.             XDiff = "+" ; ZDiff = "-"
  241.         END
  242.     END
  243.  
  244.     IF ReqHeading < 0 THEN DO
  245.         IF ReqHeading < -90 THEN DO    /* Quadrant III */
  246.             XDiff = "-" ; ZDiff = "-"
  247.         END
  248.         IF ReqHeading >= -90 THEN DO   /* Quadrant IV  */
  249.             XDiff = "-" ; ZDiff = "+"
  250.         END
  251.     END
  252.                                 /* Calculating distance between points  */
  253.     Skew = ABS(ReqDistance * COS((ReqPitch * DegToRad)))
  254.     Height = ABS(ReqDistance * SIN((ReqPitch * DegToRad)))
  255.     Width = ABS(Skew * SIN((ReqHeading * DegToRad)))
  256.     Depth = ABS(Skew * COS((ReqHeading * DegToRad)))
  257.  
  258.     IF ReqCalc = 1 THEN DO              /* Calculate Position of Point A */
  259.         INTERPRET "XA = "||XB||XDiff||Width
  260.         INTERPRET "YA = "||YB||YDiff||Height
  261.         INTERPRET "ZA = "||ZB||ZDiff||Depth
  262.     END
  263.  
  264.     IF ReqCalc = 2 THEN DO              /* Calculate Position of Point B */
  265.         INTERPRET "XB = "||XA||XDiff||Width
  266.         INTERPRET "YB = "||YA||YDiff||Height
  267.         INTERPRET "ZB = "||ZA||ZDiff||Depth
  268.     END
  269.  
  270. RETURN
  271.  
  272. /* -------------------------------------------------------------------- */
  273.                                                      /* Calculating HPD */
  274. CalcHPD:
  275.  
  276.     Width = ABS((XB-XA)) ; Height = ABS((YB-YA)) ; Depth = ABS((ZB-ZA))
  277.     Skew = SQRT((Width * Width) + (Depth * Depth))
  278.     ReqDistance = SQRT((Skew * Skew) + (Height * Height))
  279.  
  280.     IF Width ~= 0 THEN ReqHeading = ATAN(Depth / Width) * RadToDeg
  281.     IF Width = 0 THEN ReqHeading = 0
  282.     IF Skew ~= 0 THEN ReqPitch = ATAN(Height / Skew) * RadToDeg
  283.     IF Skew = 0 THEN ReqPitch = 0
  284.  
  285.     IF XB > XA THEN DO
  286.         IF ZB > ZA THEN ReqHeading = 90 - ReqHeading
  287.         IF ZB < ZA THEN ReqHeading = ReqHeading + 90
  288.     END
  289.     IF XB < XA THEN DO
  290.         IF ZB > ZA THEN ReqHeading = ReqHeading - 90
  291.         IF ZB < ZA THEN ReqHeading = (90 - ReqHeading) - 180
  292.     END
  293.  
  294.     IF YB > YA THEN ReqPitch = ReqPitch - ReqPitch - ReqPitch
  295.  
  296. RETURN
  297.  
  298. /* -------------------------------------------------------------------- */
  299.                                                      /* Creating Points */
  300. CreatePoints:
  301.  
  302.     CALL Add_Begin()
  303.     CALL Add_Point(XA YA ZA)
  304.     CALL Add_Point(XB YB ZB)
  305.     CALL Add_Polygon(1 2)
  306.     CALL Add_End()
  307.  
  308. RETURN
  309.  
  310. /* -------------------------------------------------------------------- */
  311.                                                               /* Ending */
  312. Exiting:
  313.  
  314.     IF (VT3DLib) THEN CALL REMLIB("LWModelerARexx.port")
  315.     EXIT
  316.  
  317. RETURN
  318.  
  319. /* -------------------------------------------------------------------- */
  320.                                                  /* Information Windows */
  321. InformationWindows:
  322.  
  323.     OKorCancel = Notify(2, Info1A, Info1B, Info1C, Info1D, Info1E, Info1F, Info1G, Info1H, Info1I, Info1J, Info1K, Info1L, Info1M, Info1N)
  324.     IF OKorCancel = 1 THEN CALL Notify(1, Info2A, Info2B, Info2C, Info2D, Info2E, Info2F, Info2G, Info2H, Info2I, Info2J, Info2K, Info2L, Info2M, Info2N)
  325.     CALL MenuRequester
  326.  
  327. RETURN                                           /* Not actually needed */
  328.  
  329. /* -------------------------------------------------------------------- */
  330.                                             /* Recording Macro Settings */
  331. SaveSettings:
  332.  
  333.     IF (OPEN(PrefsFile, PrefsFileName, 'W')) THEN DO
  334.         CALL WRITELN(PrefsFile, MacrosName)
  335.  
  336.         CALL WRITELN(PrefsFile, XA YA ZA)
  337.         CALL WRITELN(PrefsFile, XB YB ZB)
  338.         CALL WRITELN(PrefsFile, ReqHeading ReqPitch ReqDistance)
  339.         CALL WRITELN(PrefsFile, ReqCalc)
  340.         CALL WRITELN(PrefsFile, BEHInfoReqCreate)
  341.  
  342.         CALL CLOSE PrefsFile
  343.     END
  344.  
  345. RETURN
  346.  
  347. /* -------------------------------------------------------------------- */
  348.                                                       /* Error Handling */
  349. SYNTAX:
  350. ERROR:
  351.  
  352.     ErrCode = RC
  353.     ErrLine = SIGL
  354.     ErrInfo = ERRORTEXT(ErrCode)
  355.  
  356.     Err1 = "!Sorry!"
  357.     Err2 = "An Error has been detected"
  358.     Err3 = "@þ Macro -            "
  359.     Err4 = "@þ Line Number -      "
  360.     Err5 = "@þ Error Code -       "
  361.     Err6 = "@þ Error Description -"
  362.     Err7 = "@¤ Please Inform -    "
  363.     Err8 = '  "Error Notice"     '
  364.     Err9 = "  13032 Copenhill Rd."
  365.     Err10 = "  Dallas, TX. 75240  "
  366.  
  367.     Call Notify(1,Err1,Err2,Err3,MacrosName,Err4,ErrLine,Err5,ErrCode,Err6,ErrInfo,Err7,Err8,Err9,Err10)
  368.  
  369. /* -------------------------------------------------------------------- */
  370.                                              /* Advanced Error Handling */
  371.     CALL SETCLIP("ErrorMacro",MacrosName)
  372.     CALL SETCLIP("ErrorLine",ErrLine)
  373.     CALL SETCLIP("ErrorCode",ErrCode)
  374.     CALL SETCLIP("ErrorDesc",ErrInfo)
  375.  
  376.     PARSE SOURCE TempA TempB ErrFile TempC TempD TempE
  377.  
  378.     CALL SETCLIP("ErrorFile",ErrFile)
  379.  
  380. /* -------------------------------------------------------------------- */
  381.  
  382.     IF (VT3DLib) THEN CALL REMLIB("LWModelerARexx.port")
  383.     EXIT
  384.