     @tubeRadius = 0.1
     @isim = new CircularInterpolatingSimulator simulator, order, interpSteps, smoothing
 
-    @stepZ = 1 / interpSteps
-
     @chunkSize = 200
     @stepZ = 0.1
-    @scale = scale = 30
     @nCells = pattern.length
-    @colors = (palette[i%palette.length] for i in [0...@nCells] by 1)
-    @group = new THREE.Object3D
-    @chunks = []
-    @materials = for color in @colors
-      new THREE.MeshBasicMaterial color: color
-
-    @zMin = -100
-    @lastChunkZ = 0
     @jumpTreshold = 3
-
-    @group.scale.set scale, scale, scale
-    @group.position.set -0.5*simulator.width*scale, -0.5*simulator.height*scale, 0
-    @group.updateMatrix()
-    
-        
-  makeChunk: ->
+    @lastState = null
+
+  makeChunkBlueprint: ->
     unless @lastState
       @lastState = @isim.getInterpolatedState()
     states = for i in [0...@chunkSize] by 1
       @isim.nextTime 1
       @isim.getInterpolatedState()
     #create lines
-    chunk = new THREE.Object3D
-    for i in [0...@nCells]
-      tubeGeom = @createTube states, i*2, @lastState
-      tube = new THREE.Mesh tubeGeom, @materials[i]
-      chunk.add tube
+    tubes = for i in [0...@nCells]
+      @makeTubeBlueprint states, i*2, @lastState
     @lastState = states[states.length-1]
-    return chunk
-
-
+    return tubes
+  chunkLen: -> @chunkSize * @stepZ
   makeTubeBlueprint: (xys, i, xy0)->
     jumpTreshold = @jumpTreshold
     vs = new Float32Array xys.length*4*3 #x,y,z; 4 vertices

    #Offset to put pattern to the center
    cx = ((simulator.width - patW)/2) & ~1
    cy = ((simulator.height - patH)/2) & ~1
    console.log "Putting pattern at #{cx}, #{cy}"
    simulator.put pattern, cx, cy #pattern roughly at the center
    
    order = 3

      idx_used: curIx
    return blueprint
    
  

class ChunkedFlyingCurves
  constructor: ->
    @tubing = tubing = new Tubing
    
    @scale = scale = 30

    @colors = (palette[i%palette.length] for i in [0...tubing.nCells] by 1)
    @group = new THREE.Object3D
    @chunks = []
    @materials = for color in @colors
      new THREE.MeshBasicMaterial color: color

    @zMin = -100
    @lastChunkZ = 0

    @group.scale.set scale, scale, scale
    simulator = @tubing.isim.simulator
    @group.position.set -0.5*simulator.width*scale, -0.5*simulator.height*scale, 0
    @group.updateMatrix()
    
        
  makeChunk: ->
    blueprint = @tubing.makeChunkBlueprint()

    #create lines
    chunk = new THREE.Object3D
    for tubeBp, i in blueprint
      tubeGeom = @createTube tubeBp
      tube = new THREE.Mesh tubeGeom, @materials[i]
      chunk.add tube
    return chunk


  createTube: (blueprint)->
    tube = new THREE.BufferGeometry()

    vs = blueprint.v.subarray 0, blueprint.v_used
