Scenegraph - Tutorial - Using Def/Use Constructs

The 'def' and 'use' constructs are used together to allow us to include a branch of the scenegraph multiple times in the scene.

example def use In this example we have a branch that has this house (the one on the left). This is under a 'def' node which gives it a name ("house"). The other 3 branches contain a 'use' node which is set to use the def node.

There is no great efficiency saving here as it would have been just as easy to include 'addScenePattern' multiple times. However, this could have been some very complicated branch of the scenegraph, in which case it is much more efficient to define it only once and then use it again where required. Here is the code that produced this diagram:

(1) -> DF ==> DoubleFloat
                                                             Type: Void

(2) -> PT ==> SCartesian(2)
                                                             Type: Void

(3) -> view := boxBoundary(sipnt(0,-500)$PT,sipnt(1200,500)$PT)
   (3)  bound box:pt(0.0,- 500.0)->pt(1200.0,500.0)
                                         Type: SBoundary(SCartesian(2))

(4) -> sc := createSceneRoot(view)$Scene(PT)
   (4)  scene root bound box:pt(0.0,- 500.0)->pt(1200.0,500.0) #ch=0
                                             Type: Scene(SCartesian(2))

(5) -> SH := createSceneTransform(_
       stransform([[0.1::DF,0::DF,0::DF],_
                    [0::DF,0.1::DF,0::DF],_
                    [0::DF,0::DF,1::DF]]_
                      )$STransform(PT))$Scene(PT)

                              +0.1  0.0  0.0+
   (5)  scene transform tr=mtx|0.0  0.1  0.0| #ch=0
                              +0.0  0.0  1.0+
                                             Type: Scene(SCartesian(2))

(6) -> H := addScenePattern(SH,"HOUSE"::Symbol,2,view)
   (6)
   scene line
     [[pt(0.0,- 500.0),pt(0.0,- 260.0),....],
      [pt(180.0,- 500.0),pt(240.0,- 500.0),....], ....]
      #ch=0
                                             Type: Scene(SCartesian(2))

(7) -> addSceneDef(sc,"house",SH)
   (7)  scene def nn="house" #ch=0
                                             Type: Scene(SCartesian(2))

(8) -> OFSET1 := addSceneTransform(sc,_
       stransform([[1::DF,0::DF,200::DF],_
                    [0::DF,1::DF,100::DF],_
                    [0::DF,0::DF,1::DF]]_
                      )$STransform(PT))$Scene(PT)

                              +1.0  0.0  200.0+
   (8)  scene transform tr=mtx|0.0  1.0  100.0| #ch=0
                              +0.0  0.0   1.0 +
                                             Type: Scene(SCartesian(2))

(9) -> addSceneUse(OFSET1,"house")
   (9)  scene use nn="house" #ch=0
                                             Type: Scene(SCartesian(2))

(10) -> OFSET2 := addSceneTransform(sc,_
       stransform([[1::DF,0::DF,400::DF],_
                    [0::DF,1::DF,-100::DF],_
                    [0::DF,0::DF,1::DF]]_
                      )$STransform(PT))$Scene(PT)

                               +1.0  0.0   400.0 +
   (10)  scene transform tr=mtx|0.0  1.0  - 100.0| #ch=0
                               +0.0  0.0    1.0  +
                                             Type: Scene(SCartesian(2))

(11) -> addSceneUse(OFSET2,"house")
   (11)  scene use nn="house" #ch=0
                                             Type: Scene(SCartesian(2))

(12) -> OFSET3 := addSceneTransform(sc,_
       stransform([[1::DF,0::DF,600::DF],_
                    [0::DF,1::DF,100::DF],_
                    [0::DF,0::DF,1::DF]]_
                      )$STransform(PT))$Scene(PT)

                               +1.0  0.0  600.0+
   (12)  scene transform tr=mtx|0.0  1.0  100.0| #ch=0
                               +0.0  0.0   1.0 +
                                             Type: Scene(SCartesian(2))

(13) -> addSceneUse(OFSET3,"house")
   (13)  scene use nn="house" #ch=0
                                             Type: Scene(SCartesian(2))

(14) -> writeSvg(sc,"testGraph/exampleuse.svg")
                                                             Type: Void

End users don't need to know about how it works but internally it uses the 'SceneNamedPoints(PT)' domain which associates a part of the scenegraph with a name as follows:

(15) -> NB := namedBranch([],[])$SceneNamedPoints(PT)
   (15)  br:
                                  Type: SceneNamedPoints(SCartesian(2))

(16) -> addNode!(NB,"a",H)
   (16)  nd:"a"
                                  Type: SceneNamedPoints(SCartesian(2))

(17) -> findNode(NB,"a")
   (17)
   scene line
     [[pt(0.0,- 500.0),pt(0.0,- 260.0),....],
      [pt(180.0,- 500.0),pt(240.0,- 500.0),....], ....]
      #ch=0
                                             Type: Scene(SCartesian(2))

metadata block
see also:
Correspondence about this page

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2023 Martin John Baker - All rights reserved - privacy policy.