Rad CAD By Matthew J. W. Ratcliff 09-APR-90 CAD Package Documentation TO RUN Rad CAD Documentation First load your current MSMOUSE driver, capable of supporting VGA graphics mode. To RUN Rad CAD insert the Rad CAD RUN DISK in the floppy drive and enter: RADCAD at the DOS prompt. The program will first present a title screen, press [ENTER]. The program will detect graphics modes supported by your computer and present you with a menu of graphics options. If your machine supports it, select 'V' for VGA graphics, 640x480. NOTE: Rad CAD does NOT take aspect ratio into account. CIRCLES will look like true circles only in VGA mode. In EGA and CGA modes, some distortion will occur. Future revisions of Rad CAD may take this into consideration. A 'dialog' box will appear, showing some information about the program and author. Point and click on the [OK] button, or click anywhere on the screen, to erase the dialog box and continue. The main menu bar will come with the following entries: [Create] [Erase] [Objects] [File] [Misc] [Exit] NOTE: Click the LEFT button to PICK an item. The RIGHT button is usually reserved to CANCEL a selection. For certain object CREATION sequences the RIGHT button means DONE. These options will be explained in detail later. To see an animation demonstration, first point and click the left button on the [FILE] menu. Click on the [LOAD] option. In the file selection dialog, point to DOG.CAD and click the LEFT mouse button. A picture of a dog sitting in a room, with a window in the background, and a ball above its head, will be loaded and displayed. Next click on the [OBJECTS] menu, highlight the [ANIMATE] option, and click. The dog will begin wagging its tail, its eyes will move, its ears will move up and down, as the ball bounces up and down on its head. Click the mouse button to stop the animation. (It make several tries to get it to recognize your button presses, as it is quite busy with the animation. Keep clicking, it will recognize it.) To see another animation demonstration, select [LOAD] from the [FILE] menu, accept the [OVERWRITE] option, and then click on BFLY.CAD. Then select [ANIMATE] from the [OBJECTS] menu to see the butterfly flap its wings. Next, to see a complex 3D rendering of multiple images, select [LOAD] from the [FILE] menu. Accept the [OVERWRITE] option, and then click on 3DPICS.CAD. A picture with a dozen different 3D objects will be loaded and displayed. TO COMPILE Rad CAD Documentation You must have my GRAPHICS.LIB file, VIDEOID.OBJ, and all the .C and .H files from the following page in a directory on the hard drive. The file RADCAD.PRJ should be in this same directory. Make this directory the current directory on your system. Then execute the following command: TC RADCAD Turbo C will start up in the interactive environment, and load RADCAD.C. Next press Alt-P for PROJECT and select the file RADCAD.PRJ as the project file. Then select Alt-O, for options. Press C for COMPILER options, and select the MEMORY MODEL. Choose the LARGE memory model for this program. Select Alt-C to compile, and choose COMPILE TO EXE file. Rad CAD will probably be too large to run from the Turbo C interactive environment. Press Alt-X to exit Turbo C and then type RADCAD at the next DOS prompt to run the newly compiled program. File Documentation FILENAME FUNCTION CIRCLE.C Generate a circle using Bresenham's algorithm. CREATE.C Create menu user interface and implementation. ERASE.C Erase menu user interface and implementation. FILE.C File menu user interface and implementation. FILES.C Directory list support utility. LAB.C Common LAB support functions. RADCAD.C Create MAIN MENU user interface for Rad CAD, and miscellaneous support routines. MATRIX3D.C 3D Matrix and Vector operations package. MENU.C Horizontal and Vertical menu user interface support. MISC.C Miscelaneous menu user interface and implementation. MOUSE.C Mouse operations package, provided. OBJECTS.C Objects menu user interface and implementation. OBJOPS.C Object operations package. Interface between menu implementations and MATRIX3D package. Inputs: user information (such as angles and screen coordinates). Outputs: New objects, after appropriate transforms. CIRCLE.H Include file for CIRCLE.C function. CREATEV.H Create menu variables. ERASEV.H Erase menu varaibles. FILES.H Include file for FILES.C function. FILEV.H File menu variables. LAB.H Include file for LAB.C functions. RADCAD.H Include file for Rad CAD functions and structures. MATRIX3D.H Include file for MATRIX3D functions. MENU.H Include file for MENU.C functions and constants. MENUSV.H Rad CAD main menu variables. MISCV.H Miscellaneous menu variables. MOUSE.H Include file for MOUSE.C functions and constants. OBJDEFS.H Object definitions file with constants and primary OBJECT_TYPE data structure definitions. OBJECTSV.H Objects menu variables. OBJOPS.H Include file for OBJOPS.C functions and constants. SPCLMENU.H Special menus include file implemented in RADCAD.C. VIDEOID.H Interface to VIDEOID.ASM. USING Rad CAD by M. Ratcliff CREATE A 3D OBJECT Point to the CREATE menu and click on POLYGON. Click on the OPEN option. Draw half the cut away outline of an object, a wine glass, for example. Start drawing the polygon by positioning the mouse pointer and clicking the left button. Click the left button at to set more points. Click the right button when the polygon is complete. After finishing the polygon click on DONE (or anywhere on the screen outside of CREATE menu area) to close the CREATE sub-menu. Point to the OBJECTS menu entry and click to open the menu. Select the 3-D option. You are first prompted to select the object to 'SPIN-3D'. Point to the object you just drew and click the left mouse button. Clicking the right mouse button will cancel the 3-D operation. The selected object is highlighed by drawing a rectangle around the area the object covers. Its geometric center and center x, and y positions on the rectangle's edges, are highlighted. A prompt at the bottom of the display appears as follows: Draw AXIS of ROTATION, LEFT-end pts The left mouse button sets the axis' end points. The right button will cancel the 3-D operation. After drawing the axis, another OBJECTS sub-menu will drop down. It will request the step angle of rotation. This is the distance the object's vertices are rotated before creating a new edge. The menu allows selection of varying increments to create an object with 4 to 36 sides. It was found that creating 72 or more sides took a prohibitively long time, and did not improve the look of the final product. (Hidden line removal, a math coprocessor, and surface shading would change that, however.) Select the step angle of rotation by clicking the left mouse button. The right will cancel the operation. A feedback message appears at the bottom of the display, indicating that a new 3-D object is being created. When complete, the new 3-D image is drawn. Now to rotate the object, to look at the bottom of the glass, for example, select ROTATE from the OBJECTS menu. You are prompted to select the object to ROTATE. Point at the glass and click the left button to select, the right button will cancel the operation. The object is highlighted with a selection box. Then another menu drops down below the OBJECTS menu, prompting to rotate about the X, Y, or Z axis. Select the Y axis, and click the left button, for example. (Remember the RIGHT button will CANCEL the operation.) You are next prompted at the bottom of the display to select the center of rotation. The mouse pointer is set to the center of the selection box for the object. As you move the pointer a rubberband line is drawn from that center to the current position. Click the left button to set the center of rotation. The right button will cancel the rotation operation. The reference line will remain on the display, along with a small box marking the center of rotation to be used. Next, a prompt will appear at the bottom of the display, requesting an angle of rotation. To the rotate bottom, counterclockwise toward you (assuming the glass is oriented diagonally, with the top at the upper right of the display), enter an angle of 35 degrees. Type 35 and press ENTER. A feedback message is displayed, indicating the new rotation is being calculated. When complete, the object is redrawn at the new position. Take notice of the status line at the bottom of the display. It indicates that it is still in the rotate selection mode - select another object to rotate with the left button, or the right button to cancel and go back to the OBJECTS submenu. Select the OBJECTS submenu again, and click on UNDO. Click on the glass you just rotated. It immediately goes back to its previous position. Point and click the left button again, and your rotation is restored. Click the right button when done UNDOing. Select the OBJECTS submenu and click on ANIMATE. The glass will continuously bounce back and forth between the rotated and unrotated states. Any objects that have been manipulated by most transformations in the OBJECTS menu will have an active "Undo pointer". Animate continuously draws objects, erases them, draws their undo object, erases them, and starts over - until you press either mouse button to interrupt the animation sequence. Try loading the files DOG.CAD, BFLY.CAD, or BOUNCE.CAD, and selecting ANIMATE for them, to see some more complex examples. Future enhancements might include user programmable animation, where you may specify how many levels deep an "Undo pointer" sequence might grow. This would allow you to generate an object, and continuously translate, scale, and rotate it, while the program buffers all the creation steps. Then animate might play back a space ship spinning off into the distance, for example. Another feature to be added is the ability to select any single object and eliminate its "Undo pointer" without affecting others. (Notice in DOG.CAD how the dog and ball move, but the rest of the objects are static.) Rad CAD - MENUS CREATE LINE - Use the left button to set successive end points to line segments. Each line is a separate, disconnected object. Click the right button when done drawing lines. POLYGON - When selected another menu pops up, requesting an open or closed polygon. Use the left button to set vertices on the polygon object. Click the right button when done drawing. If closed polygon type was selected, the polygon is automatically closed for you. If closed polygon was requested, but there are less than 3 points already specified, a status message is displayed to this effect and the object is canceled. A multipoint polygon is a single object. CIRCLE - A sub-menu pops up for the CIRCLE selection. The degree step between connection points on the circle is requested. A triangle may be created by selecting 120 degrees, for example. (360/120 = 3 sides = a triangle) Angle steps are provided for pentagon, ehxagon, and octagon as well as circle approximations with 18, 36, and 72 total sides. Even for very large circles, 72 sides (increments of 5 degrees for each vertice), results in a pretty smooth looking circle. Creating a circle is a 3 step sequence. First click the left mouse button to set the center of the circle. Then a "rubberband circle" is drawn from that center in response to your mouse movements relative to that center. The radius is taking from the x position of the mouse pointer to the right of the center. Click the left button to set the circle's size, and the rubberband circle is replaced by a "drag circle". A circle of the size specified follows the mouse pointer around on the display. The mouse pointer becomes the dynamic center of the circle. This allows for fine positioning of the circle after having set its size. Click the left button to make the final placement of the circle. Click the right button at any time in this sequence to cancel the creation of the circle object. After completing the circle creation, the circle is generated (with appropriate feedback messages), and drawn permanently on the display. At this point click the left button to set the center of yet another circle, or the right button to stop creating circles and return to the CREATE submenu. BOX - The box creation sequence is much the same as the circle. First click to set the center. Then click to set the size. Then click to set the final location of the box. Notice that when creating a box, uniform scaling of the box's size is enforced by the program. The right button is used to cancel creation of a box. After completing a box creation, the left button continues with another box, the right indicates you are done and stops the box create sequence. RECTANGLE - This is identical to BOX, except that the scaling is differential, allowing the mouse pointer to control height and width of the object dynamically, once the center has been set. PAINT - The paint mode allows you to use the mouse pointer as a stylus, a continuous drawing device. When PAINT is selected another sub-menu pops up for the PAINT THINNER. This can be set to none, or to keep one in 5, 10, or 20 points. When painting, you can create a great many points in a very small area. This eats a lot of memory, and doesn't always look good when creating a 3-D object from a PAINT object. The PAINT THINNER selection of 1 in 10, for example, will take a completed PAINT STROKE and keep only 1 in 10 points created, plus the two end points. You will notice some shape change in the object, if paint thinning is enabled. Click the left button to start painting continuously. Click the left button a second time to complete the paint stroke. Click the right button between paint strokes, when you are done. This will return control to the CREATE sub-menu. Each paint stroke is a separate object, and is treated as an open polygon type. POINTS - When this option is selected another submenu is displayed requesting that each point be created as a seaparte object, or that they be grouped as a single object. If OBJECT GROUP is selected, you can create a series of points that may be selected together as one large object. Should you select a point group to SPIN 3-D, they will automatically be connected as a an open polygon in the 3-D generation sequence. SEPARATE OBJECTS will result in a separate object structure for each point. Each point may then be translated or rotated individually. Note that a single point object cannot be scaled. In the POINTS mode, use the left button to set points. Use the right button when done creating points. HELP and DONE - These should be self explanitory. Any time a drop down menu is on the display, you may click on DONE to eliminate it, and take control of the top menu bar for the next selection. However, you may also simply click anywhere on the display, outside of a current drop down menu, to achieve the same effect. Rad CAD MENUS ERASE ERASE ALL - Erase all objects in the current drawing. A confirmation menu is displayed. The user must click on YES! to accept or CANCEL to stop the erase step. This is a 'soft delete'. Objects may be selectively restored from the OBJECTS menu with the UNERASE option. ERASE ALL & CLEANUP - A lot of memory can be occupied by deleted and undo object pointers. This option will not only erase all objects, but will also free all associated memory. This is the option to use when you wish to start with a completely clean slate. (This is equivalent to selecting ERASE ALL from the ERASE menu, and then CLEANUP/UNDO & ERASE, from the OBJECTS menu.) This option too has a second confirmation menu, for safety. ERASE OBJECTS - Point and click the left button to select an object. Then click the left button to confirm its erasure, or the right button to cancel. Click the right button, without selecting an object, to exit the erase objects mode. These erasures are 'soft deletes' too, and may be retores with UNERASE from the OBJECTS menu. Rad CAD MENUS OBJECTS ROTATE - Rotate an object about the X, Y, or Z axis. After selecting ROTATE, you are prompted to select the object to rotate. Point and click with the left button to highlight and select the object to rotate, the right button to cancel. The "Rotate about..." sub-menu then pops up prompting for the X, Y, or Z axis of rotation. The mouse pointer is then set to the center of the object's selection box, and you are prompted to draw the center of rotation. Simply click the left button to accept the object's center, or move the mouse pointer to the desired center of rotation and click the left button. A rubberband line is drawn to show the relative rotation axis. When the center of rotation is set, a small box is drawn at the reference point. The status line then displays a request for the rotation angle. Type the angle of rotation, in degrees, and press enter. The object is then rotated about the selected reference point, by the ammount specified, and redrawn. MOVE - Translate an object. Point at the object to translate and click the left button. A highlight box is shown around the object. Click the right button if it is the wrong object and try again with the left button. If you have the object you desire, move the mouse pointer to the new center for the object. A drag-box, the size of the original selection box, follows the mouse pointer as it is moved. Click the left button to set the new center. The program then performs the translation and redraws the object at its new location. SCALE - A "Scale Options" menu is presented when SCALE is selected. You may select Uniform or Differential scaling. When uniform scaling is selected, the same scale factor is used along both the X and Y axes of the object. Differential scaling allows you to stretch or compress the object in either direction. Point to the object to scale and click the left mouse button to select, the right to cancel. A highlight box is drawn around the object. Move the mouse pointer, referenced to the bottom right of the box, to change the size. If uniform scaling was selected, the software will control the mouse pointers position to some extent - to ensure uniform scaling. When the relative box size is correct, click the left button to complete the scaling sequence or the right to cancel. The program will compute the scale factor and resize the object, and display it, accordingly. Click the left button on another object to scale it, or the right button to return to the OBJECTS menu. 3-D - Select an object to SPIN about an arbitrary axis. The user first selects the object with the left button. You are next prompted to draw an axis of rotation by setting the end points of a line with the left button. Then a step angle of rotation must be selected from a sub-menu. The step angle determines the total sides of the object. Both angles and total sides are displayed in the menu. Simply highlight your preference and click the left button. Between 15 and 20 sides are best for most rotations. You may wish to use 36 sides for very large objects, and only 10 to 12 for smaller ones. You may generate as few as 4 sides to a 3D object. Note that no object can have more than 32K bytes of data in it. This is a limitation of the PC and the LARGE memory model. We could go to the HUGE memory model for larger objects, but this would require that all array indices in the software to be recoded as LONG instead of INT. The software would be significantly slower and much larger, as well. Therefore, this program checks the total memory size required by a new 3D object. If that object will have 32K or more of data, a warning message is displayed, and the object isn't created. You can try again by selecting a fewer number of sides for the object to SPIN 3D. COPY - Select an object to make a duplicate copy of. The process is nearly identical to MOVE. Select an object to copy. Position a "drag box" at the new location, and a duplicate copy of the object is created at the new position. In the file BFLY.CAD, I drew only one butterfly wing. I then used COPY to duplicate it. ROTATE was then used to rotate the wing abour the Y axis 180 degrees to generate a mirror copy to complete the pair of wings. ANIMATE - Animate the display by continuously drawing all objects' "Undo pointer" backup copies, and then the current copies. This is demonstrated in the sample files DOG.CAD, BFLY.CAD, and BOUNCE.CAD, on the Rad CAD RUN disk. Click the mouse button several times to stop the animation sequence. UNDO - If you perform a transformation on any object and change your mind, select UNDO. Point to the object and click the left button to undo it. Click the left button again, to undo the undo. UNERASE - Review all objects previously erased. Click the left button to restore a previously erased object. Click the right button to leave it erased. For multiple erased objects, you must continue clicking the right button to step through all of them. The OBJECTS menu will come back after all erased objects have been reviewed. CLEANUP - Every object that has been transformed has an active "Undo" pointer, that is a backup copy. Also, every object that has been "soft erased" still exists. This eats up a lot of memory. When working on large, complex objects (or when you want to start fresh on a new animation sequence), you may wish to use CLEANUP. A sub-menu is presented. You may clean up UNDO memory, ERASE memory, or BOTH UNDO and ERASE memory. Any erased objects or undo pointers are freed permenantly and cannot be recovered if these options are used. REDRAW - Sometimes the display can become a bit muddled. Occasiontally object selection boxes will leave a few points behind, or eraseure of an object will conflict erase part of another. This program generally does a very good job of keeping the display clean and current. For those few times when it does lag behind, select REDRAW to refresh the entire display. Rad CAD MENUS FILE SAVE - Save the current drawing to a file. The default extender is .CAD. When SAVE is selected, the program finds all files in the current directory and displays them in a 10 file list window. If there are more than 10 files, you can click on NEXT PAGE and PREVIOUS PAGE selections in the file select dialog box. You may also click on "enter new name" to send control to the status line at the bottom of the screen. Here you can type in a new file name. Simply type the name and press ENTER. The program will add the .CAD extender for you. Or, if you wish to overwrite another CAD file, simply click on that filename. LOAD - Load a new drawing file. If there are already objects on the display you are prompted to OVERWRITE, MERGE, or CANCEL the file load. If you select OVERWRITE, all objects are erased (permanently), and then the LOAD FILENAME menu pops up. You can click on the desired file to load. As objects are extracted from the file, they are displayed. If the MERGE option is chosen, the objects from the file are added to the objects on the display. If you load objects in a different resolution (say CGA) than the one in which they were created (say VGA), they are automatically scaled as they are loaded from the file, to fit on the current display. If saved again, they retain the scale factors for the current resulution. Rad CAD MENUS MISC Miscellaneous Functions INFO - Show program information. Display program name, author, last update, and revision number. Click any mouse button anywhere to continue. MOUSE COORDS - Display a sub-menu allowing the user to enable or disable the display of mouse coordinates while drawing. Disabling them speeds up the object creation process a bit. MENU SPEED - Display a sub-menu allowing the selection of Normal, Pretty, or Fast, Ugly menus. Fast menus eliminate the pretty background fills, but do speed up the menu operation a bit. A nice feature for slower PC's. NUM POINTS - Select an object, highlight it, and show the total number of points in its structure. The point count is shown on the status line at the bottom of the screen. Click the right button when done. Rad CAD MENUS EXIT EXIT - A sub-menu is displayed allowing the user to EXIT NOW or CANCEL, and continue drawing. Rad CAD SOFTWARE METRICS LOC, Lines Of Code, is a software metrics utility I wrote. It will report lines of code, comments, blank lines, and relative percentages, on any type of software. The program allows you to specify the format of the comment structure of the language you are counting lines of code for. It allows you to specify file groups, with wild cards. There are 31 different .C and .H files requred to build Rad CAD, a total of 12036 lines of code, 51% code, 30% comments, and the balance blank lines. A complete LOC summary is included elsewhere in this package. Rad CAD HIERARCHY HIER, Hierarchy chart generator, is a program written by Clare Strebel of McDonnell Douglas. This program will check a group of C or PL/1 source and include files, generate a dependencies map file, and then output a complete hierarchy chart for each source file. The output of this program for all files in Rad CAD is located elsewhere in this package. Rad CAD LISTINGS Following the LOC and HIER reports are the program listings. The first program listing is RADCAD.C. After initialization, notice that the main loop of the program (page 7) is only half a page long. All listings following RADCAD.C are in alphabetical order, the .C files first, and then the .H include files. Each drop down menu is a 'handler' module. The source files are CREATE.C, ERASE.C, OBJECTS.C, FILE.C, and MISC.C. The main loop for each menu in each of these files is also brief and to the point. Each 'handler' has local procedures to take care of each menu selection besides Help and Done, which are handled in the main loop. CREATE - The local procedures in CREATE work with CreateObject() in the file OBJOPS.C, to create new objects. All memory allocation is dynamic. If you run out of memory at any time, the user is notified, and the creation is aborted. There should be no unexpected "crashes" from running out of memory. All memory manipulation for objects is carried out in one module, CreateObject(). This centralized control helped contain bugs during the development process. Memory is freed for other functions and object creation with the DestroyObject() procedure, also in OBJOPS.C. OBJECTS - The local procedures in this package first interact with the user to collect necessary data to perform a selected transformation. Once all data is accepted, it is passed to a support routine in OBJOPS.C, such as TranslateObject(). The OBJOPS package interfaces with the MATRIX3D package to get transformation matrices, and perform necessary matrix and vector multiplications. MATRIX3D - This is a 3D matrix operations package. Before I could fully define all the data structures for this project, I needed a better understanding of the transformations involved. So, I wrote MATRIX3D. It generates all the 4x4 transformation matrices, implements matrix and vector-matrix multiplication. Debugging support routines were added to print out a vector or matrix. I then created sample vectors and matrices and performed the operations with a test program. The output of MATRIX3D was compared with Math Cad 2.0 for accuracy. Once satisfied with that, I was able to develop the data structures for the objects, and bulid up from there to the already completed user interface (from LAB3 and LAB4). OBJDEFS.H - In this file is the definition of the data structrue OBJECT_TYPE. It contains essential information for every object type the program supports. The pointers to vPts (vector points) and sPts (screen points) are dynamically allocated. They are set to the size necessary for an object requested. Most transformations result in the same number of output points, excepting SPIN 3D. CreateObject() handles these problems with the alloc() and realloc() functions of Turbo C. The structure of OBJECT_TYPE contains BOTH the homogeneous (3D) vector coordinates and display mapped screen coordinates. This enables rapid screen redraws and simple animation sequences by simply drawing directly from the sPts[] array list in the OBJECT_TYPE. Any time a new transform is required, the vPts[] array is manipulated. For each homogeneous coordinate, a call to MapToScreen() in OBJOPS.C is made to transform the VECTOR3D_TYPE coordinate to the display, VIEWO_COORD_TYPE. FILE - When a file is saved, both the object and its undo object are written to a file. Thus, animation sequences become part of a file. If you wish to save only a static image, then select CLEANUP before saving to a file. Note, that if you load a file that hasn't been cleaned up first, then you effectively can still Undo any transformation. The data written to the file includes the screen resolution used to create the objects, total objects (not counting undo connections), and essential information for each object. This includes the object identifier, object operation last performed, undo connection, if any, and VETOR3D_TYPE point data. The screen point data is not saved. When an object file is loaded, screen coordinates are recalculated, after rescaling to a new screen size, if necessary. Rad CAD - FUTURE ENHANCEMENTS As I learn more about Interactive 3D Graphics, I plan to continue enhancing Rad CAD. New features may include: o Optional hidden line removal. o Surface shading, shadows, and light sources. o Ray traced rendering. o Vector format text, allowing the placement of text on the display, with rotation, scaling, and so on. o Special line types, such as dashed, center line, arrows, attached dimensions. o User specified world coordinates. o 3-D clipped perspective progjections. o User controlled animation levels and playback sequences. o Support for Super-VGA graphics modes, up to 1024 by 768. o Interactive editing of objects, inserting, deleting, and moving points within an object structure. o Dot matrix, plotter, and laser printer output.