File: SVEventImplC.mesa
Last edited by Bier on July 12, 1987 1:54:27 pm PDT
Copyright © 1984 by Xerox Corporation. All rights reserved.
Contents: Procedures for responding to button clicks made in a solidviewer.
Bloomenthal, June 16, 1987 7:52:46 pm PDT
DIRECTORY
BasicTime, CoordSys, DisplayListToTree, Feedback, Imager, ImagerInterpress, IO, Matrix3d, Preprocess3d, Rope, SV2d, SV3d, SVAssembly, SVBasicTypes, SVCaret, SVCastRays, SVEditUser, SVEvent, SVInterfaceTypes, SVMasterObject, SVMasterObjectTypes, SVModelTypes, SVRay, SVRefresh, SVScene, SVSceneTypes, SVSelect, SVSelections, SVState, SVTransforms, SVUserInput, SVUtility, SVVector3d, SVViewersOnScene, SVViewerTools, SVWindow, TIPUser, ViewerClasses, ViewerTools;
SVEventImplC: CEDAR PROGRAM
IMPORTS BasicTime, CoordSys, DisplayListToTree, Feedback, Imager, ImagerInterpress, IO, Matrix3d, Preprocess3d, SVAssembly, SVCaret, SVCastRays, SVEditUser, SVEvent, SVRay, SVRefresh, SVScene, SVSelect, SVSelections, SVState, SVTransforms, SVUtility, SVVector3d, SVViewersOnScene, SVViewerTools, SVWindow, ViewerTools
EXPORTS SVEvent =
BEGIN
Artwork: TYPE = SVModelTypes.Artwork;
ArtworkToolData: TYPE = SVInterfaceTypes.ArtworkToolData;
Slice: TYPE = SVSceneTypes.Slice;
SliceList: TYPE = SVSceneTypes.SliceList;
BoundBox: TYPE = SVBasicTypes.BoundBox;
BoundSphere: TYPE = SVBasicTypes.BoundSphere;
Camera: TYPE = SVModelTypes.Camera;
Classification: TYPE = SVSceneTypes.Classification;
Color: TYPE = Imager.Color;
CoordSystem: TYPE = SVModelTypes.CoordSystem;
CSGTree: TYPE = SVSceneTypes.CSGTree;
CylinderRec: TYPE = SVMasterObjectTypes.CylinderRec;
EditToolData: TYPE = SVInterfaceTypes.EditToolData;
FileCamera: TYPE = SVSceneTypes.FileCamera;
FrameBox: TYPE = SVModelTypes.FrameBox;
MasterObject: TYPE = SVSceneTypes.MasterObject;
Matrix4by4: TYPE = SV3d.Matrix4by4;
Point2d: TYPE = SV2d.Point2d;
Point3d: TYPE = SV3d.Point3d;
Primitive: TYPE = SVSceneTypes.Primitive;
Ray: TYPE = SVSceneTypes.Ray;
Scene: TYPE = SVSceneTypes.Scene;
SearchDepth: TYPE = SVSceneTypes.SearchDepth;
Selection: TYPE = SVInterfaceTypes.Selection;
SelectionGenerator: TYPE = SVInterfaceTypes.SelectionGenerator;
Shape: TYPE = SVSceneTypes.Shape;
Skitter: TYPE = SVSceneTypes.Skitter;
SkitterMode: TYPE = SVSceneTypes.SkitterMode;
ToolData: TYPE = SVSceneTypes.ToolData;
TrigLine: TYPE = SV2d.TrigLine;
Vector3d: TYPE = SV3d.Vector3d;
SVData: TYPE = SVInterfaceTypes.SVData;
Select Menu
SelectAll: PUBLIC PROC [event: LIST OF REF ANY, svData: SVData] = {
editToolData: EditToolData ← svData.editToolData;
scene: Scene ← svData.scene;
SVSelect.DeselectAll[svData.scene, normal];
SVSelect.SelectAll[svData.scene, normal];
SVViewersOnScene.PaintSceneAllViewers[paintAction: $SelectionChanged, editToolData: editToolData, scene: scene, edited: FALSE];
};
Units Menu
pointsPerIn: REAL = 72.0;
pointsPerCm: REAL = 72.0/2.54;
cmPerIn: REAL = 2.54;
ScaleUnitFromSegment: PUBLIC PROC [event: LIST OF REF ANY, svData: SVData] = {
};
ScaleUnitFromValue: PUBLIC PROC [event: LIST OF REF ANY, svData: SVData] = {
};
ScaleUnitFromSelection: PUBLIC PROC [event: LIST OF REF ANY, svData: SVData] = {
};
InchScaleUnit: PUBLIC PROC [event: LIST OF REF ANY, svData: SVData] = {
ScaleUnitAux[svData, pointsPerIn]; -- one inch in screen dots
};
ScaleUnitAux: PROC [svData: SVData, distance: REAL] = {
SVState.SetScaleUnit[svData, distance]; -- in screen dots
PrintScaleUnit[event: NIL, svData: svData];
SVWindow.RestoreScreenAndInvariants[paintAction: $NewAlignmentsSelected, svData: svData, remake: alignBag, backgndOK: TRUE, edited: FALSE, okToClearFeedback: TRUE];
};
CentimeterScaleUnit: PUBLIC PROC [event: LIST OF REF ANY, svData: SVData] = {
ScaleUnitAux[svData, pointsPerCm];
};
PointsScaleUnit: PUBLIC PROC [event: LIST OF REF ANY, svData: SVData] = {
ScaleUnitAux[svData, 1];
};
PrintScaleUnit: PUBLIC PROC [event: LIST OF REF ANY, svData: SVData] = {
inches: REAL ← SVState.GetScaleUnit[svData];
Feedback.PutF[svData.feedback, oneLiner, "Current scale is %g points = %g inches = %g centimeters", [real[inches*pointsPerIn]], [real[inches]], [real[inches/cmPerIn]] ];
};
TIP Table Functions
IPSnapShot: PUBLIC PROC [event: LIST OF REF ANY, svData: SVData] = {
This command is executed in the middle of a dragging operation to make an interpress master of the current state of the screen. Luckily, all dragging operations use the same painting commands, namely:
SVWindow.RestoreScreenAndInvariants[paintAction: $DuringMotion, svData: svData, remake: none, backgndOK: TRUE, edited: FALSE, okToClearFeedback: FALSE];
We wish to have the same effect as these commands except written to an interpress master. We will create an interpress master named snapshot.ip and use SVRefresh.SnapShot to draw the scene into it.
DoMakeInterpress: PROC [dc: Imager.Context] = {
ENABLE UNWIND => {
svData.camera.displayStyle ← tempStyle;
};
DoItInterpress: PROC = {
Imager.ScaleT[dc, metersPerPixel];
svData.camera.displayStyle ← print;
SVRefresh.SnapShot[dc, svData];
svData.camera.displayStyle ← tempStyle;
};
Imager.DoSave[dc, DoItInterpress];
};
ipRef: ImagerInterpress.Ref;
fullName: Rope.ROPE;
success: BOOL;
metersPerPixel: REAL = 0.0254/72.0;
startTime: BasicTime.GMT;
endTime: BasicTime.GMT;
totalTime: INT;
msgRope: Rope.ROPE;
tempStyle: SVModelTypes.DisplayStyle ← svData.camera.displayStyle;
[fullName, success] ← SVUtility.GetInterpressFileName["snapshot.ip", svData.currentWDir, svData.feedback];
IF NOT success THEN RETURN;
ipRef ← ImagerInterpress.Create[fullName];
msgRope ← IO.PutFR["Writing to IP file: %g . . . ", [rope[fullName]]];
Feedback.Append[svData.feedback, msgRope, begin];
startTime ← BasicTime.Now[];
ImagerInterpress.DoPage[ipRef, DoMakeInterpress, 1.0];
ImagerInterpress.Close[ipRef];
endTime ← BasicTime.Now[];
totalTime ← BasicTime.Period[startTime, endTime];
msgRope ← IO.PutFR[" Done in time (%r)", [integer[totalTime]]];
Feedback.Append[svData.feedback, msgRope, end];
};
JackPivotX: PUBLIC PROC [event: LIST OF REF ANY, svData: SVData] = {
JackPivotAux[event, svData, 1];
};
JackPivotY: PUBLIC PROC [event: LIST OF REF ANY, svData: SVData] = {
JackPivotAux[event, svData, 2];
};
JackPivotZ: PUBLIC PROC [event: LIST OF REF ANY, svData: SVData] = {
JackPivotAux[event, svData, 3];
};
JackPivotAux: PUBLIC PROC [event: LIST OF REF ANY, svData: SVData, axis: [1..3]] = {
Rotate the distinguished target coincident with respect to itself by degrees. This makes sense for both hook and coordsys targets.
editToolData: EditToolData ← svData.editToolData;
scene: Scene ← svData.scene;
degrees: REALNARROW[event.rest.first, REF REAL]^;
targetSel: Selection;
coincident: Slice;
targetSel ← SVSelections.TopTarget[];
IF targetSel = NIL THEN RETURN;
coincident ← targetSel.coincident;
Erases the target shape. For a jack, this leaves nothing. For another object, this leaves the object.
SVTransforms.Rotate[coincident, scene, axis, degrees, coincident.coordSys, TRUE];
SVViewersOnScene.PaintSceneAllViewers[paintAction: $SelectionChanged, editToolData: editToolData, scene: scene, edited: TRUE];
};
SourcePivotX: PUBLIC PROC [event: LIST OF REF ANY, svData: SVData] = {
SourcePivotAux[event, svData, 1];
};
SourcePivotY: PUBLIC PROC [event: LIST OF REF ANY, svData: SVData] = {
SourcePivotAux[event, svData, 2];
};
SourcePivotZ: PUBLIC PROC [event: LIST OF REF ANY, svData: SVData] = {
SourcePivotAux[event, svData, 3];
};
SourcePivotAux: PUBLIC PROC [event: LIST OF REF ANY, svData: SVData, axis: [1..3]] = {
Rotate the distinguished source indirect with respect to the distinguished source coincident. If the selection is not a hook jack, then indirect and coincident are identical.
editToolData: EditToolData ← svData.editToolData;
scene: Scene ← svData.scene;
degrees: REALNARROW[event.rest.first, REF REAL]^;
sourceSel: Selection;
jack, indirect: Slice;
sourceSel ← SVSelections.TopMovee[];
IF sourceSel = NIL THEN RETURN;
SELECT sourceSel.referentType FROM
hook => {
jack ← sourceSel.coincident;
indirect ← sourceSel.indirect;
};
coordSys => {
jack ← sourceSel.coincident;
indirect ← sourceSel.coincident;
};
ENDCASE => ERROR;
SVViewersOnScene.SceneNewVersion[editToolData.currentSVData];
SVTransforms.Rotate[indirect, scene, axis, degrees, jack.coordSys, TRUE];
svData.refresh.addedObject ← indirect;
SVViewersOnScene.PaintSceneAllViewers[$ObjectChangedBoundBoxProvided, editToolData, scene];
};
ArrowShoot: PUBLIC PROC [event: LIST OF REF ANY, svData: SVData] = {
Cast a ray from the distinguished source to each of the targets. Create a hook at each intersection point, aligned with the surface normal at that point.
source, target: Slice;
sourceSel, firstTargetSel: Selection;
editToolData: EditToolData;
sourceSel ← SVSelections.PopMovee[];
IF sourceSel = NIL THEN RETURN;
source ← sourceSel.coincident;
svData ← sourceSel.svData;
editToolData ← svData.editToolData;
firstTargetSel ← SVSelections.PopTarget[];
IF firstTargetSel = NIL THEN RETURN;
FOR targetSel: Selection ← firstTargetSel, SVSelections.NextTarget[] UNTIL targetSel = NIL DO
IF targetSel.svData # sourceSel.svData THEN {
Feedback.Append[svData.feedback, "Can't shoot arrows between viewers.", oneLiner];
Feedback.Blink[svData.feedback];
LOOP;
};
target ← targetSel.coincident;
ArrowShootAux[source.coordSys, target.coordSys, svData];
ENDLOOP;
SVViewersOnScene.SceneNewVersion[editToolData.currentSVData];
SVViewersOnScene.PaintSceneAllViewers[$PaintEntireScene, svData.editToolData, svData.scene];
};
ArrowShootAux: PROC [sourceCS, targetCS: CoordSystem, svData: SVData] = {
Cast a ray from source to target. Create a hook at each intersection point, aligned with the surface normal at that point.
sOriginWorld, tOriginWorld: Point3d;
tree: CSGTree;
ray: Ray;
class: Classification;
t: REAL;
worldDirection, primitiveNormal, worldNormal: Vector3d;
selectionMat: Matrix4by4;
surfacePtInWorld: Point3d;
primitive: Primitive;
assembly: Slice;
scene: Scene;
camera: Camera;
skitter: Skitter ← svData.editToolData.skitter;
scene ← svData.scene;
camera ← svData.camera;
tree ← DisplayListToTree.AssemblyToTree[scene.assembly, scene, camera];
[] ← Preprocess3d.PreprocessForCatScan[tree, camera];
sOriginWorld ← Matrix3d.OriginOfMatrix[CoordSys.WRTWorld[sourceCS]];
tOriginWorld ← Matrix3d.OriginOfMatrix[CoordSys.WRTWorld[targetCS]];
ray ← SVRay.GetRayFromPool[];
worldDirection ← SVVector3d.Sub[tOriginWorld, sOriginWorld];
SVRay.StuffWorldRay[ray, sOriginWorld, worldDirection, camera];
class ← SVCastRays.RayCastBoundingSpheres[ray, tree.son];
FOR i: NAT IN [1..class.count] DO
t ← class.params[i]; -- the parameter of the ray intersection
primitiveNormal ← class.normals[i];
primitive ← class.primitives[i];
worldNormal ← Matrix3d.UpdateVectorWithInverse[primitive.worldWRTPrim, primitiveNormal];
surfacePtInWorld[1] ← sOriginWorld[1] + t*worldDirection[1];
surfacePtInWorld[2] ← sOriginWorld[2] + t*worldDirection[2];
surfacePtInWorld[3] ← sOriginWorld[3] + t*worldDirection[3];
assembly ← NARROW[primitive.assembly];
selectionMat ← MakeAlignedMat[worldNormal, surfacePtInWorld, assembly.coordSys];
SVCaret.SetAssemblyAndPrimitive[skitter, assembly, primitive];
SVCaret.PositionFromMatrix[skitter, [0,0], selectionMat];
SVSelections.SetModeSkitter[svData, surface];
SVEvent.SkitterMakes[NIL, svData];
ENDLOOP;
SVCastRays.ReturnClassToPool[class];
SVRay.ReturnRayToPool[ray];
};
Sign: PROC [r: REAL] RETURNS [INT] = {
IF r = 0.0 THEN RETURN[2];
IF r < 0.0 THEN RETURN[-1]
ELSE RETURN[1];
};
AntiParallel: PRIVATE PROC [v1, v2: Vector3d] RETURNS [BOOL] = {
RETURN[Sign[v1[1]] = -Sign[v2[1]] OR
Sign[v1[2]] = -Sign[v2[2]] OR
Sign[v1[3]] = -Sign[v2[3]] ];
};
MakeAlignedMat: PRIVATE PROC [worldNormal: Vector3d, surfacePtInWorld: Point3d, cs: CoordSystem] RETURNS [mat: Matrix4by4] = {
Create a Matrix4by4 with origin at surfacePtInWorld whose z axis is parallel to worldNormal and whose x zxis is orthogonal to both worldNormal and the y axis of cs in WORLD coordinates. Assume that CoordSys.WRTWorld[cs] is accurate
yAxisOfCS: Vector3d ← Matrix3d.YAxisOfMatrix[CoordSys.WRTWorld[cs]];
xAxis: Vector3d;
IF SVVector3d.Parallel[yAxisOfCS, worldNormal] THEN {
xAxis ← Matrix3d.XAxisOfMatrix[CoordSys.WRTWorld[cs]];
IF AntiParallel[yAxisOfCS, worldNormal] THEN xAxis ← SVVector3d.Negate[xAxis];
Allows positioning code to distinguish between top surfaces and bottom surfaces.
}
ELSE xAxis ← SVVector3d.CrossProduct[yAxisOfCS, worldNormal];
mat ← Matrix3d.MakeMatFromZandXAxis[worldNormal, xAxis, surfacePtInWorld];
};
MoveUntilTouch: PUBLIC PROC [event: LIST OF REF ANY, svData: SVData] = {
Consider a line from the distinguished source to the distinguished target. Using translation along the ray direction (only) move the distinguish source towards the distinguished target until it touches something other than itself. If the distinguished source already touches something else, leave it alone.
source, target: Slice;
sourceSel, targetSel: Selection;
editToolData: EditToolData;
sourceSel ← SVSelections.PopMovee[];
IF sourceSel = NIL THEN RETURN;
source ← sourceSel.coincident;
svData ← sourceSel.svData;
editToolData ← svData.editToolData;
targetSel ← SVSelections.PopTarget[];
IF targetSel = NIL THEN RETURN;
IF targetSel.svData # sourceSel.svData THEN {
Feedback.Append[svData.feedback, "Can't skewer between viewers.", oneLiner];
Feedback.Blink[svData.feedback];
RETURN;
};
target ← targetSel.coincident;
MoveUntilTouchAux[source, target, svData];
SVViewersOnScene.SceneNewVersion[editToolData.currentSVData];
SVViewersOnScene.PaintSceneAllViewers[$PaintEntireScene, svData.editToolData, svData.scene];
};
MoveUntilTouchAux: PROC [source, target: Slice, svData: SVData] = {
Cast a set of rays parallel to the line from source to target and starting on a plane normal to that line a bounding sphere's radius behind source. Using translation along the ray direction (only) move the source until it touches something (anything).
sOriginWorld, tOriginWorld: Point3d;
tree, sourceTree: CSGTree;
worldDirection: Vector3d;
scene: Scene;
camera: Camera;
boundSphere: BoundSphere;
R: REAL;
basisMat: Matrix4by4;
scene ← svData.scene;
camera ← svData.camera;
sourceTree ← DisplayListToTree.AssemblyToTree[source, scene, camera];
boundSphere ← Preprocess3d.PreprocessForCatScan[sourceTree, camera];
R ← boundSphere.radius;
tree ← DisplayListToTree.AssemblyToTree[scene.assembly, scene, camera];
[] ← Preprocess3d.PreprocessForCatScan[tree, camera];
sOriginWorld ← Matrix3d.OriginOfMatrix[CoordSys.WRTWorld[source.coordSys]];
tOriginWorld ← Matrix3d.OriginOfMatrix[CoordSys.WRTWorld[target.coordSys]];
worldDirection ← SVVector3d.Sub[tOriginWorld, sOriginWorld];
Shoot many rays in this direction starting from the plane of points x satisfying (x-sOriginWorld).w = 0. First, let's find two basis vectors for this plane. They are the x and y axes of the basis matrix.
basisMat ← Matrix3d.MakeHorizontalMatFromZAxis[worldDirection, sOriginWorld];
MoveUntilTouchAuxAux[source, basisMat, worldDirection, R, scene, camera, tree];
}; -- end of MoveUntilTouchAux
MoveUntilTouchAuxAux: PROC [source: Slice, basisMat: Matrix4by4, worldDirection: Vector3d, R: REAL, scene: Scene, camera: Camera, tree: CSGTree] = {
ray: Ray;
minDeltaT, lastSourceT, firstOtherT: REAL;
class: Classification;
success, someData: BOOL;
basisBasePt, worldBasePt: Point3d;
moveVector: Vector3d;
raysPerHalfSide: NAT = 20;
raysPerHalfSideF: REAL = 20.0;
ray ← SVRay.GetRayFromPool[];
someData ← FALSE;
FOR i: INT IN [-raysPerHalfSide..raysPerHalfSide-1] DO
basisBasePt[1] ← (i+0.5)*R/raysPerHalfSideF;
FOR j: INT IN [-raysPerHalfSide..raysPerHalfSide-1] DO
basisBasePt[2] ← (j+0.5)*R/raysPerHalfSideF;
basisBasePt[3] ← 0.0;
worldBasePt ← Matrix3d.Update[basisBasePt, basisMat];
SVRay.StuffWorldRay[ray, worldBasePt, worldDirection, camera];
class ← SVCastRays.RayCastBoundingSpheres[ray, tree.son, FALSE];
[lastSourceT, firstOtherT, success] ← SourceAndOtherT[class, source];
IF NOT success THEN {
SVCastRays.ReturnClassToPool[class];
LOOP;
};
IF firstOtherT < lastSourceT THEN {
Feedback.AppendRaw[$Solidviews, "Source is already touching.", oneLiner];
Feedback.BlinkRaw[$Solidviews];
RETURN;
};
IF NOT someData THEN {
minDeltaT ← firstOtherT-lastSourceT;
someData ← TRUE;
}
ELSE minDeltaT ← MIN[minDeltaT, firstOtherT-lastSourceT];
SVCastRays.ReturnClassToPool[class];
ENDLOOP; -- j
ENDLOOP; -- i
IF NOT someData THEN {
Feedback.AppendRaw[$Solidviews, "No Obstacles Found. Object NOT moved.", oneLiner];
Feedback.BlinkRaw[$Solidviews];
RETURN;
};
moveVector ← SVVector3d.Scale[worldDirection, minDeltaT];
SVTransforms.Translate[source, scene, moveVector];
SVRay.ReturnRayToPool[ray];
};
SourceAndOtherT: PROC [class: Classification, source: Slice] RETURNS [lastSourceT, firstOtherT: REAL, success: BOOLFALSE] = {
sourceFound, otherFound: BOOL;
thisAssembly: Slice;
sourceFound ← FALSE;
otherFound ← FALSE;
IF class.count = 0 THEN RETURN [0,0,FALSE];
FOR i: NAT IN [1..class.count] DO
thisAssembly ← NARROW[class.primitives[i].assembly];
IF DisplayListToTree.IsSuccessorOf[thisAssembly, source] THEN {
lastSourceT ← class.params[i];
sourceFound ← TRUE;
}
ELSE IF NOT otherFound THEN {
firstOtherT ← class.params[i];
otherFound ← TRUE;
};
ENDLOOP;
success ← sourceFound AND otherFound;
};
Skewer: PUBLIC PROC [event: LIST OF REF ANY, svData: SVData] = {
Shoot a ray from the distinguished source to the distinguished target. Using translation along the ray direction (only) bring all intersected objects together so they are touching at the ray intersection points.
source, target: Slice;
sourceSel, firstTargetSel: Selection;
editToolData: EditToolData;
sourceSel ← SVSelections.PopMovee[];
IF sourceSel = NIL THEN RETURN;
source ← sourceSel.coincident;
svData ← sourceSel.svData;
editToolData ← svData.editToolData;
firstTargetSel ← SVSelections.PopTarget[];
IF firstTargetSel = NIL THEN RETURN;
IF firstTargetSel.svData # sourceSel.svData THEN {
Feedback.Append[svData.feedback, "Can't skewer between viewers.", oneLiner];
Feedback.Blink[svData.feedback];
RETURN;
};
target ← firstTargetSel.coincident;
SkewerAux[source, target, svData];
SVViewersOnScene.SceneNewVersion[editToolData.currentSVData];
SVViewersOnScene.PaintSceneAllViewers[$PaintEntireScene, svData.editToolData, svData.scene];
};
SkewerAux: PROC [source, target: Slice, svData: SVData] = {
Cast a ray from source to target. Using translation along the ray direction (only) bring all intersected objects together so they are touching at the ray intersection points. The skewer extends from the earliest point hit on the source object (by the infinite line) to the last point hit on the target object. For now, a kludge: Cast a ray from 30% behind the source to 30% after the target.
sOriginWorld, tOriginWorld: Point3d;
tree: CSGTree;
ray: Ray;
class: Classification;
t: REAL;
directionWorld: Vector3d;
surfacePtInWorld, lastSurfacePtInWorld: Point3d;
primitive: Primitive;
assembly, lastAssembly: Slice;
moveVector, totalVector, localDiff: Vector3d;
scene: Scene;
camera: Camera;
sourceCS, targetCS: CoordSystem;
sourceCS ← source.coordSys;
targetCS ← target.coordSys;
scene ← svData.scene;
camera ← svData.camera;
tree ← DisplayListToTree.AssemblyToTree[scene.assembly, scene, camera];
[] ← Preprocess3d.PreprocessForCatScan[tree, camera];
sOriginWorld ← Matrix3d.OriginOfMatrix[CoordSys.WRTWorld[sourceCS]];
tOriginWorld ← Matrix3d.OriginOfMatrix[CoordSys.WRTWorld[targetCS]];
ray ← SVRay.GetRayFromPool[];
directionWorld ← SVVector3d.Sub[tOriginWorld, sOriginWorld];
Here comes the kludge
sOriginWorld ← SVVector3d.Sub[sOriginWorld, SVVector3d.Scale[directionWorld, 0.3]];
directionWorld ← SVVector3d.Scale[directionWorld, 1.6];
End of kludge.
SVRay.StuffWorldRay[ray, sOriginWorld, directionWorld, camera];
class ← SVCastRays.RayCastBoundingSpheres[ray, tree.son, FALSE];
SVCastRays.SortClassByPrimitive[class];
IF class.count = 0 THEN {
SVCastRays.ReturnClassToPool[class];
SVRay.ReturnRayToPool[ray];
RETURN;
};
lastAssembly ← NARROW[class.primitives[1].assembly];
t ← class.params[1];
lastSurfacePtInWorld[1] ← sOriginWorld[1] + t*directionWorld[1];
lastSurfacePtInWorld[2] ← sOriginWorld[2] + t*directionWorld[2];
lastSurfacePtInWorld[3] ← sOriginWorld[3] + t*directionWorld[3];
totalVector ← [0,0,0];
FOR i: NAT IN [2..class.count] DO
t ← class.params[i]; -- the parameter of the ray intersection
IF t > 1.0 THEN EXIT; -- Compression occurs only BETWEEN source and target.
primitive ← class.primitives[i];
surfacePtInWorld[1] ← sOriginWorld[1] + t*directionWorld[1];
surfacePtInWorld[2] ← sOriginWorld[2] + t*directionWorld[2];
surfacePtInWorld[3] ← sOriginWorld[3] + t*directionWorld[3];
assembly ← NARROW[primitive.assembly];
IF lastAssembly # assembly THEN {
localDiff ← SVVector3d.Sub[lastSurfacePtInWorld, surfacePtInWorld];
moveVector ← SVVector3d.Add[localDiff, totalVector];
totalVector ← SVVector3d.Add[totalVector, localDiff];
SVTransforms.Translate[assembly, scene, moveVector];
};
lastAssembly ← assembly;
lastSurfacePtInWorld ← surfacePtInWorld;
ENDLOOP;
SVCastRays.ReturnClassToPool[class];
SVRay.ReturnRayToPool[ray];
};
AddCylinder: PUBLIC PROC [event: LIST OF REF ANY, svData: SVData] = {
Add a cylinder whose radius is taken from the editTool and whose height is just large enough so it stretches from the distinguished source to the distinguished target.
sourceSel, firstTargetSel: Selection;
sourceCS, targetCS: CoordSystem;
editToolData: EditToolData;
radius: REAL;
scene: Scene;
sourceSel ← SVSelections.PopMovee[];
IF sourceSel = NIL THEN RETURN;
firstTargetSel ← SVSelections.PopTarget[];
IF firstTargetSel = NIL THEN RETURN;
IF sourceSel.svData # firstTargetSel.svData THEN {
Feedback.Append[svData.feedback, "Can't add cylinder between viewers.", oneLiner];
Feedback.Blink[svData.feedback];
RETURN;
};
svData ← sourceSel.svData;
scene ← svData.scene;
editToolData ← svData.editToolData;
sourceCS ← sourceSel.coincident.coordSys;
radius ← SVViewerTools.GetReal[editToolData.cylinderSection.radius, 20];
FOR targetSel: Selection ← firstTargetSel, SVSelections.NextTarget[] UNTIL targetSel = NIL DO
targetCS ← targetSel.coincident.coordSys;
AddCylinderAux[sourceCS, targetCS, radius, editToolData, scene];
ENDLOOP;
SVViewersOnScene.SceneNewVersion[editToolData.currentSVData];
SVViewersOnScene.PaintSceneAllViewers[$PaintEntireScene, editToolData, svData.scene];
};
AddCylinderAux: PROC [sourceCS, targetCS: CoordSystem, radius: REAL, editToolData: EditToolData, scene: Scene] = {
Add a cylinder whose radius is taken from the editTool and whose height is just large enough so it stretches from sourceCS to targetCS.
cylMO: MasterObject;
moFound: BOOL;
cylinderRec: CylinderRec;
sOriginWorld, tOriginWorld, midPointWorld: Point3d;
cylWorld, superWorld, cylSuper: Matrix4by4;
cylinderY: Vector3d;
height: REAL;
newAssembly, superAssembly: Slice;
success: BOOL;
addSucceeds: BOOLTRUE;
cylName: Rope.ROPE;
[cylMO, moFound] ← SVScene.FindObjectFromName["cylinder", scene];
IF NOT moFound THEN ERROR;
cylinderRec ← NARROW[cylMO.mainBody];
sOriginWorld ← Matrix3d.OriginOfMatrix[CoordSys.WRTWorld[sourceCS]];
tOriginWorld ← Matrix3d.OriginOfMatrix[CoordSys.WRTWorld[targetCS]];
midPointWorld ← SVVector3d.Add[sOriginWorld, tOriginWorld];
midPointWorld ← SVVector3d.Scale[midPointWorld, 0.5];
cylinderY ← SVVector3d.Sub[tOriginWorld, sOriginWorld];
height ← SVVector3d.Magnitude[cylinderY];
cylWorld ← MakeHorizontalMatFromYAxis[cylinderY, midPointWorld];
radius ← radius/cylinderRec.radius; height ← height/cylinderRec.height;
cylName ← ViewerTools.GetContents[editToolData.sceneSection.new];
cylName ← CoordSys.UniqueNameFrom[cylName, scene.coordSysRoot];
[superAssembly, success] ← SVEditUser.GetParent[editToolData];
IF NOT success THEN RETURN;
superWorld ← CoordSys.WRTWorld[superAssembly.coordSys];
cylSuper ← Matrix3d.AInTermsOfB[cylWorld, superWorld];
[newAssembly, ----, addSucceeds] ← SVAssembly.CreatePrimitive[cylName, "cylinder", scene];
IF NOT addSucceeds THEN RETURN;
addSucceeds ← SVAssembly.AddPrimitive[newAssembly, [radius, height, radius], superAssembly, cylSuper, scene];
}; -- end of AddCylinderAux
MakeHorizontalMatFromYAxis: PROC [yAxis: Vector3d, origin: Point3d] RETURNS [mat: Matrix4by4] = {
Uses yAxis as it is. Finds a horizontal x axis orthogonal to zAxis. If yAxis is vertical, then there are infinitely many. Chooses [1,0,0] in this case.
xAxis: Vector3d;
IF yAxis[1] = 0 AND yAxis[3] = 0 THEN xAxis ← [1,0,0]
ELSE xAxis ← SVVector3d.CrossProduct[[0,1,0], yAxis];
mat ← Matrix3d.MakeMatFromYandXAxis[yAxis, xAxis, origin];
};
END.