Class: VA3C

Inherits:
Object
  • Object
show all
Defined in:
lib/measures/view_data/resources/va3c.rb,
lib/measures/view_model/resources/va3c.rb

Overview

Va3c class converts an OpenStudio model to vA3C JSON format for rendering in Three.js using export at va3c.github.io/projects/#./osm-data-viewer/latest/index.html# as a guide many thanks to Theo Armour and the vA3C team for figuring out many of the details here

Defined Under Namespace

Classes: AmbientLight, Geometry, GeometryData, Material, Scene, SceneChild, SceneObject, UserData, Vertex

Class Method Summary collapse

Class Method Details

.build_materials(model) ⇒ Object

create the standard materials



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/measures/view_data/resources/va3c.rb', line 144

def self.build_materials(model)
  materials = []

  # materials << make_material('Undefined', format_color(255, 255, 255), 1, THREE::DoubleSide)
  materials << { uuid: format_uuid(OpenStudio.createUUID).to_s, name: 'Undefined', type: 'MeshBasicMaterial', color: '0xffffff'.hex, side: THREE::DoubleSide }

  materials << make_material('NormalMaterial', format_color(255, 255, 255), 1, THREE::DoubleSide)
  # materials << make_material('NormalMaterial_Ext', format_color(255, 255, 255), 1, THREE::FrontSide)
  materials << { uuid: format_uuid(OpenStudio.createUUID).to_s, name: 'NormalMaterial_Ext', type: 'MeshBasicMaterial', color: '0xffffff'.hex, side: THREE::FrontSide }
  materials << make_material('NormalMaterial_Int', format_color(255, 0, 0), 1, THREE::BackSide)

  # materials from 'openstudio\openstudiocore\ruby\openstudio\sketchup_plugin\lib\interfaces\MaterialsInterface.rb'
  materials << make_material('Floor', format_color(128, 128, 128), 1, THREE::DoubleSide)
  materials << make_material('Floor_Ext', format_color(128, 128, 128), 1, THREE::FrontSide)
  materials << make_material('Floor_Int', format_color(191, 191, 191), 1, THREE::BackSide)

  materials << make_material('Wall', format_color(204, 178, 102), 1, THREE::DoubleSide)
  materials << make_material('Wall_Ext', format_color(204, 178, 102), 1, THREE::FrontSide)
  materials << make_material('Wall_Int', format_color(235, 226, 197), 1, THREE::BackSide)

  materials << make_material('RoofCeiling', format_color(153, 76, 76), 1, THREE::DoubleSide)
  materials << make_material('RoofCeiling_Ext', format_color(153, 76, 76), 1, THREE::FrontSide)
  materials << make_material('RoofCeiling_Int', format_color(202, 149, 149), 1, THREE::BackSide)

  materials << make_material('Window', format_color(102, 178, 204), 0.6, THREE::DoubleSide)
  materials << make_material('Window_Ext', format_color(102, 178, 204), 0.6, THREE::FrontSide)
  materials << make_material('Window_Int', format_color(192, 226, 235), 0.6, THREE::BackSide)

  materials << make_material('Door', format_color(153, 133, 76), 1, THREE::DoubleSide)
  materials << make_material('Door_Ext', format_color(153, 133, 76), 1, THREE::FrontSide)
  materials << make_material('Door_Int', format_color(202, 188, 149), 1, THREE::BackSide)

  materials << make_material('SiteShading', format_color(75, 124, 149), 1, THREE::DoubleSide)
  materials << make_material('SiteShading_Ext', format_color(75, 124, 149), 1, THREE::FrontSide)
  materials << make_material('SiteShading_Int', format_color(187, 209, 220), 1, THREE::BackSide)

  materials << make_material('BuildingShading', format_color(113, 76, 153), 1, THREE::DoubleSide)
  materials << make_material('BuildingShading_Ext', format_color(113, 76, 153), 1, THREE::FrontSide)
  materials << make_material('BuildingShading_Int', format_color(216, 203, 229), 1, THREE::BackSide)

  materials << make_material('SpaceShading', format_color(76, 110, 178), 1, THREE::DoubleSide)
  materials << make_material('SpaceShading_Ext', format_color(76, 110, 178), 1, THREE::FrontSide)
  materials << make_material('SpaceShading_Int', format_color(183, 197, 224), 1, THREE::BackSide)

  materials << make_material('InteriorPartitionSurface', format_color(158, 188, 143), 1, THREE::DoubleSide)
  materials << make_material('InteriorPartitionSurface_Ext', format_color(158, 188, 143), 1, THREE::FrontSide)
  materials << make_material('InteriorPartitionSurface_Int', format_color(213, 226, 207), 1, THREE::BackSide)

  # start textures for boundary conditions
  materials << make_material('Boundary_Surface', format_color(0, 153, 0), 1, THREE::DoubleSide)
  materials << make_material('Boundary_Adiabatic', format_color(255, 101, 178), 1, THREE::DoubleSide)
  materials << make_material('Boundary_Space', format_color(255, 0, 0), 1, THREE::DoubleSide)
  materials << make_material('Boundary_Outdoors', format_color(163, 204, 204), 1, THREE::DoubleSide)
  materials << make_material('Boundary_Outdoors_Sun', format_color(40, 204, 204), 1, THREE::DoubleSide)
  materials << make_material('Boundary_Outdoors_Wind', format_color(9, 159, 162), 1, THREE::DoubleSide)
  materials << make_material('Boundary_Outdoors_SunWind', format_color(68, 119, 161), 1, THREE::DoubleSide)
  materials << make_material('Boundary_Ground', format_color(204, 183, 122), 1, THREE::DoubleSide)
  materials << make_material('Boundary_Foundation', format_color(117, 30, 122), 1, THREE::DoubleSide)
  materials << make_material('Boundary_Groundfcfactormethod', format_color(153, 122, 30), 1, THREE::DoubleSide)
  materials << make_material('Boundary_Groundslabpreprocessoraverage', format_color(255, 191, 0), 1, THREE::DoubleSide)
  materials << make_material('Boundary_Groundslabpreprocessorcore', format_color(255, 182, 50), 1, THREE::DoubleSide)
  materials << make_material('Boundary_Groundslabpreprocessorperimeter', format_color(255, 178, 101), 1, THREE::DoubleSide)
  materials << make_material('Boundary_Groundbasementpreprocessoraveragewall', format_color(204, 51, 0), 1, THREE::DoubleSide)
  materials << make_material('Boundary_Groundbasementpreprocessoraveragefloor', format_color(204, 81, 40), 1, THREE::DoubleSide)
  materials << make_material('Boundary_Groundbasementpreprocessorupperwall', format_color(204, 112, 81), 1, THREE::DoubleSide)
  materials << make_material('Boundary_Groundbasementpreprocessorlowerwall', format_color(204, 173, 163), 1, THREE::DoubleSide)
  materials << make_material('Boundary_Othersidecoefficients', format_color(63, 63, 63), 1, THREE::DoubleSide)
  materials << make_material('Boundary_Othersideconditionsmodel', format_color(153, 0, 76), 1, THREE::DoubleSide)

  # make construction materials
  model.getConstructionBases.each do |construction|
    color = construction.renderingColor
    if color.empty?
      color = OpenStudio::Model::RenderingColor.new(model)
      construction.setRenderingColor(color)
    else
      color = color.get
    end
    name = "Construction_#{construction.name}"
    materials << make_material(name, format_color(color.renderingRedValue, color.renderingGreenValue, color.renderingBlueValue), color.renderingAlphaValue / 255.to_f, THREE::DoubleSide)
  end

  # make thermal zone materials
  model.getThermalZones.each do |zone|
    color = zone.renderingColor
    if color.empty?
      color = OpenStudio::Model::RenderingColor.new(model)
      zone.setRenderingColor(color)
    else
      color = color.get
    end
    name = "ThermalZone_#{zone.name}"
    materials << make_material(name, format_color(color.renderingRedValue, color.renderingGreenValue, color.renderingBlueValue), color.renderingAlphaValue / 255.to_f, THREE::DoubleSide)
  end

  # make space type materials
  model.getSpaceTypes.each do |spaceType|
    color = spaceType.renderingColor
    if color.empty?
      color = OpenStudio::Model::RenderingColor.new(model)
      spaceType.setRenderingColor(color)
    else
      color = color.get
    end
    name = "SpaceType_#{spaceType.name}"
    materials << make_material(name, format_color(color.renderingRedValue, color.renderingGreenValue, color.renderingBlueValue), color.renderingAlphaValue / 255.to_f, THREE::DoubleSide)
  end

  # make building story materials
  model.getBuildingStorys.each do |buildingStory|
    color = buildingStory.renderingColor
    if color.empty?
      color = OpenStudio::Model::RenderingColor.new(model)
      buildingStory.setRenderingColor(color)
    else
      color = color.get
    end
    name = "BuildingStory_#{buildingStory.name}"
    materials << make_material(name, format_color(color.renderingRedValue, color.renderingGreenValue, color.renderingBlueValue), color.renderingAlphaValue / 255.to_f, THREE::DoubleSide)
  end

  return materials
end

.build_scene(model) ⇒ Object



862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
# File 'lib/measures/view_data/resources/va3c.rb', line 862

def self.build_scene(model)
  materials = build_materials(model)

  object = {}
  object[:uuid] = format_uuid(OpenStudio.createUUID)
  object[:type] = 'Scene'
  object[:matrix] = identity_matrix
  object[:children] = []

  floor_material = materials.find { |m| m[:name] == 'Floor' }
  wall_material = materials.find { |m| m[:name] == 'Wall' }
  roof_material = materials.find { |m| m[:name] == 'RoofCeiling' }
  window_material = materials.find { |m| m[:name] == 'Window' }
  door_material = materials.find { |m| m[:name] == 'Door' }
  site_shading_material = materials.find { |m| m[:name] == 'SiteShading' }
  building_shading_material = materials.find { |m| m[:name] == 'BuildingShading' }
  space_shading_material = materials.find { |m| m[:name] == 'SpaceShading' }
  interior_partition_surface_material = materials.find { |m| m[:name] == 'InteriorPartitionSurface' }

  # loop over all surfaces
  all_geometries = []
  model.getSurfaces.each do |surface|
    material = nil
    surfaceType = surface.surfaceType.upcase
    if surfaceType == 'FLOOR'
      material = floor_material
    elsif surfaceType == 'WALL'
      material = wall_material
    elsif surfaceType == 'ROOFCEILING'
      material = roof_material
    end

    geometries, user_datas = make_geometries(surface)
    if geometries
      geometries.each_index do |i|
        geometry = geometries[i]
        user_data = user_datas[i]

        all_geometries << geometry

        scene_child = SceneChild.new
        scene_child.uuid = format_uuid(OpenStudio.createUUID)
        scene_child.name = user_data[:name]
        scene_child.type = 'Mesh'
        scene_child.geometry = geometry[:uuid]

        if i == 0
          # first geometry is base surface
          scene_child.material = material[:uuid]
        else
          # sub surface
          if /Window/.match(user_data[:surfaceType]) || /Glass/.match(user_data[:surfaceType])
            scene_child.material =  window_material[:uuid]
          else
            scene_child.material =  door_material[:uuid]
          end
        end

        scene_child.matrix = identity_matrix
        scene_child.userData = user_data
        object[:children] << scene_child.to_h
      end
    end
  end

  # loop over all shading surfaces
  model.getShadingSurfaces.each do |surface|
    geometries, user_datas = make_shade_geometries(surface)
    if geometries
      geometries.each_index do |i|
        geometry = geometries[i]
        user_data = user_datas[i]

        material = nil
        if /Site/.match(user_data[:surfaceType])
          material = site_shading_material
        elsif /Building/.match(user_data[:surfaceType])
          material = building_shading_material
        elsif /Space/.match(user_data[:surfaceType])
          material = space_shading_material
        end

        all_geometries << geometry

        scene_child = SceneChild.new
        scene_child.uuid = format_uuid(OpenStudio.createUUID)
        scene_child.name = user_data[:name]
        scene_child.type = 'Mesh'
        scene_child.geometry = geometry[:uuid]
        scene_child.material = material[:uuid]
        scene_child.matrix = identity_matrix
        scene_child.userData = user_data
        object[:children] << scene_child.to_h
      end
    end
  end

  # loop over all interior partition surfaces
  model.getInteriorPartitionSurfaces.each do |surface|
    geometries, user_datas = make_interior_partition_geometries(surface)
    geometries.each_index do |i|
      geometry = geometries[i]
      user_data = user_datas[i]

      material = interior_partition_surface_material

      all_geometries << geometry

      scene_child = SceneChild.new
      scene_child.uuid = format_uuid(OpenStudio.createUUID)
      scene_child.name = user_data[:name]
      scene_child.type = 'Mesh'
      scene_child.geometry = geometry[:uuid]
      scene_child.material = material[:uuid]
      scene_child.matrix = identity_matrix
      scene_child.userData = user_data
      object[:children] << scene_child.to_h
    end
  end

  # light = AmbientLight.new
  # light.uuid = "#{format_uuid(OpenStudio::createUUID)}"
  # light.type = "AmbientLight"
  # light.color = "0xFFFFFF".hex
  # light.matrix = identity_matrix
  # object[:children] << light.to_h

  scene = Scene.new
  scene.geometries = all_geometries
  scene.materials = materials
  scene.object = object

  return scene
end

.convert_model(model) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/measures/view_data/resources/va3c.rb', line 65

def self.convert_model(model)
  scene = build_scene(model)

  northAxis = -model.getBuilding.northAxis

  boundingBox = OpenStudio::BoundingBox.new
  boundingBox.addPoint(OpenStudio::Point3d.new(0, 0, 0))
  boundingBox.addPoint(OpenStudio::Point3d.new(1, 1, 1))
  model.getPlanarSurfaceGroups.each do |group|
    boundingBox.add(group.transformation * group.boundingBox)
  end

  lookAtX = 0 # (boundingBox.minX.get + boundingBox.maxX.get) / 2.0
  lookAtY = 0 # (boundingBox.minY.get + boundingBox.maxY.get) / 2.0
  lookAtZ = 0 # (boundingBox.minZ.get + boundingBox.maxZ.get) / 2.0
  lookAtR = [Math.sqrt((boundingBox.maxX.get / 2.0)**2 + (boundingBox.maxY.get / 2.0)**2 + (boundingBox.maxZ.get / 2.0)**2),
             Math.sqrt((boundingBox.minX.get / 2.0)**2 + (boundingBox.maxY.get / 2.0)**2 + (boundingBox.maxZ.get / 2.0)**2),
             Math.sqrt((boundingBox.maxX.get / 2.0)**2 + (boundingBox.minY.get / 2.0)**2 + (boundingBox.maxZ.get / 2.0)**2),
             Math.sqrt((boundingBox.maxX.get / 2.0)**2 + (boundingBox.maxY.get / 2.0)**2 + (boundingBox.minZ.get / 2.0)**2),
             Math.sqrt((boundingBox.minX.get / 2.0)**2 + (boundingBox.minY.get / 2.0)**2 + (boundingBox.maxZ.get / 2.0)**2),
             Math.sqrt((boundingBox.minX.get / 2.0)**2 + (boundingBox.maxY.get / 2.0)**2 + (boundingBox.minZ.get / 2.0)**2),
             Math.sqrt((boundingBox.maxX.get / 2.0)**2 + (boundingBox.minY.get / 2.0)**2 + (boundingBox.minZ.get / 2.0)**2),
             Math.sqrt((boundingBox.minX.get / 2.0)**2 + (boundingBox.minY.get / 2.0)**2 + (boundingBox.minZ.get / 2.0)**2)].max

  boundingBoxHash = { 'minX' => boundingBox.minX.get, 'minY' => boundingBox.minY.get, 'minZ' => boundingBox.minZ.get,
                      'maxX' => boundingBox.maxX.get, 'maxY' => boundingBox.maxY.get, 'maxZ' => boundingBox.maxZ.get,
                      'lookAtX' => lookAtX, 'lookAtY' => lookAtY, 'lookAtZ' => lookAtZ, 'lookAtR' => lookAtR }

  buildingStoryNames = []
  model.getBuildingStorys.each do |buildingStory|
    buildingStoryNames << buildingStory.name.to_s
  end
  buildingStoryNames.sort! { |x, y| x.upcase <=> y.upcase } # case insensitive sort

  # build up the json hash
  result = {}
  result['metadata'] = { 'version' => 4.3, 'type' => 'Object', 'generator' => 'OpenStudio',
                         'buildingStoryNames' => buildingStoryNames, 'boundingBox' => boundingBoxHash,
                         'northAxis' => northAxis }
  result['geometries'] = scene.geometries
  result['materials'] = scene.materials
  result['object'] = scene.object

  return result
end

.flatten_vertices(vertices) ⇒ Object

flatten array of vertices into a single array



280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/measures/view_data/resources/va3c.rb', line 280

def self.flatten_vertices(vertices)
  result = []
  vertices.each do |vertex|
    # result << vertex.x
    # result << vertex.y
    # result << vertex.z

    result << vertex.x.round(3)
    result << vertex.z.round(3)
    result << -vertex.y.round(3)
  end
  return result
end

.format_color(r, g, b) ⇒ Object

format color



117
118
119
# File 'lib/measures/view_data/resources/va3c.rb', line 117

def self.format_color(r, g, b)
  return "0x#{r.to_s(16).rjust(2, '0')}#{g.to_s(16).rjust(2, '0')}#{b.to_s(16).rjust(2, '0')}"
end

.format_uuid(uuid) ⇒ Object

format a uuid



112
113
114
# File 'lib/measures/view_data/resources/va3c.rb', line 112

def self.format_uuid(uuid)
  return uuid.to_s.gsub('{', '').gsub('}', '')
end

.get_vertex_index(vertex, vertices, tol = 0.001) ⇒ Object

get the index of a vertex out of a list



269
270
271
272
273
274
275
276
277
# File 'lib/measures/view_data/resources/va3c.rb', line 269

def self.get_vertex_index(vertex, vertices, tol = 0.001)
  vertices.each_index do |i|
    if OpenStudio.getDistance(vertex, vertices[i]) < tol
      return i
    end
  end
  vertices << vertex
  return (vertices.length - 1)
end

.identity_matrixObject



858
859
860
# File 'lib/measures/view_data/resources/va3c.rb', line 858

def self.identity_matrix
  return [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
end

.make_geometries(surface) ⇒ Object

turn a surface into geometries, the first one is the surface, remaining are sub surfaces



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'lib/measures/view_data/resources/va3c.rb', line 295

def self.make_geometries(surface)
  geometries = []
  user_datas = []

  # get the transformation to site coordinates
  site_transformation = OpenStudio::Transformation.new
  building = surface.model.getBuilding

  space = surface.space
  if space.is_initialized
    site_transformation = building.transformation * space.get.transformation
  else
    site_transformation = building.transformation
  end

  # get the vertices
  surface_vertices = surface.vertices
  t = OpenStudio::Transformation.alignFace(surface_vertices)
  r = t.rotationMatrix
  tInv = t.inverse
  surface_vertices = OpenStudio.reverse(tInv * surface_vertices)

  # get vertices of all sub surfaces
  sub_surface_vertices = OpenStudio::Point3dVectorVector.new
  sub_surfaces = surface.subSurfaces
  sub_surfaces.each do |sub_surface|
    sub_surface_vertices << OpenStudio.reverse(tInv * sub_surface.vertices)
  end

  # triangulate surface
  triangles = OpenStudio.computeTriangulation(surface_vertices, sub_surface_vertices)
  if triangles.empty?
    puts "Failed to triangulate surface #{surface.name} with #{sub_surfaces.size} sub surfaces"
    return geometries
  end

  all_vertices = []
  face_indices = []
  triangles.each do |vertices|
    vertices = site_transformation * t * vertices
    # normal = site_transformation.rotationMatrix*r*z

    # https://github.com/mrdoob/three.js/wiki/JSON-Model-format-3
    # 0 indicates triangle
    # 16 indicates triangle with normals
    face_indices << 0
    vertices.reverse_each do |vertex|
      face_indices << get_vertex_index(vertex, all_vertices)
    end

    # convert to 1 based indices
    # face_indices.each_index {|i| face_indices[i] = face_indices[i] + 1}
  end

  data = GeometryData.new
  data.vertices = flatten_vertices(all_vertices)
  data.normals = []
  data.uvs = []
  data.faces = face_indices
  data.scale = 1
  data.visible = true
  data.castShadow = true
  data.receiveShadow = false
  data.doubleSided = true

  geometry = Geometry.new
  geometry.uuid = format_uuid(surface.handle)
  geometry.type = 'Geometry'
  geometry.data = data.to_h
  geometries << geometry.to_h

  surface_user_data = UserData.new
  surface_user_data.handle = format_uuid(surface.handle)
  surface_user_data.name = surface.name.to_s
  surface_user_data.coincidentWithOutsideObject = false
  surface_user_data.surfaceType = surface.surfaceType
  surface_user_data.surfaceTypeMaterialName = surface.surfaceType

  surface_user_data.outsideBoundaryCondition = surface.outsideBoundaryCondition
  adjacent_surface = surface.adjacentSurface
  if adjacent_surface.is_initialized
    surface_user_data.outsideBoundaryConditionObjectName = adjacent_surface.get.name.to_s
    surface_user_data.outsideBoundaryConditionObjectHandle = format_uuid(adjacent_surface.get.handle)

    other_site_transformation = OpenStudio::Transformation.new
    other_space = adjacent_surface.get.space
    if !other_space.empty?
      other_site_transformation = building.transformation * other_space.get.transformation
    else
      other_site_transformation = building.transformation
    end

    other_vertices = other_site_transformation * adjacent_surface.get.vertices
    if OpenStudio.circularEqual(site_transformation * surface.vertices, OpenStudio.reverse(other_vertices))
      # puts "adjacent surfaces are coincident"
      surface_user_data.coincidentWithOutsideObject = true # controls display only, not energy model
    else
      # puts "adjacent surfaces are not coincident"
      surface_user_data.coincidentWithOutsideObject = false # controls display only, not energy model
    end

  end
  surface_user_data.sunExposure = surface.sunExposure
  surface_user_data.windExposure = surface.windExposure

  if surface.outsideBoundaryCondition == 'Outdoors'
    if surface.sunExposure == 'SunExposed' && surface.windExposure == 'WindExposed'
      surface_user_data.boundaryMaterialName = 'Boundary_Outdoors_SunWind'
    elsif surface.sunExposure == 'SunExposed'
      surface_user_data.boundaryMaterialName = 'Boundary_Outdoors_Sun'
    elsif surface.sunExposure == 'WindExposed'
      surface_user_data.boundaryMaterialName = 'Boundary_Outdoors_Wind'
    else
      surface_user_data.boundaryMaterialName = 'Boundary_Outdoors'
    end
  else
    surface_user_data.boundaryMaterialName = 'Boundary_' + surface.outsideBoundaryCondition
  end

  construction = surface.construction
  if construction.is_initialized
    surface_user_data.constructionName = construction.get.name.to_s
    surface_user_data.constructionMaterialName = 'Construction_' + construction.get.name.to_s
  end

  space = surface.space
  if space.is_initialized
    space = space.get
    surface_user_data.spaceName = space.name.to_s

    thermal_zone = space.thermalZone
    if thermal_zone.is_initialized
      surface_user_data.thermalZoneName = thermal_zone.get.name.to_s
      surface_user_data.thermalZoneMaterialName = 'ThermalZone_' + thermal_zone.get.name.to_s
    end

    space_type = space.spaceType
    if space_type.is_initialized
      surface_user_data.spaceTypeName = space_type.get.name.to_s
      surface_user_data.spaceTypeMaterialName = 'SpaceType_' + space_type.get.name.to_s
    end

    building_story = space.buildingStory
    if building_story.is_initialized
      surface_user_data.buildingStoryName = building_story.get.name.to_s
      surface_user_data.buildingStoryMaterialName = 'BuildingStory_' + building_story.get.name.to_s
    end
  end

  # vertices = []
  # surface.vertices.each do |v|
  #  vertex = Vertex.new
  #  vertex.x = v.x
  #  vertex.y = v.y
  #  vertex.z = v.z
  #  vertices << vertex.to_h
  # end
  # surface_user_data.vertices = vertices
  user_datas << surface_user_data.to_h

  # now add geometry for each sub surface
  sub_surfaces.each do |sub_surface|
    # triangulate sub surface
    sub_surface_vertices = OpenStudio.reverse(tInv * sub_surface.vertices)
    triangles = OpenStudio.computeTriangulation(sub_surface_vertices, OpenStudio::Point3dVectorVector.new)

    all_vertices = []
    face_indices = []
    triangles.each do |vertices|
      vertices = site_transformation * t * vertices
      # normal = site_transformation.rotationMatrix*r*z

      # https://github.com/mrdoob/three.js/wiki/JSON-Model-format-3
      # 0 indicates triangle
      # 16 indicates triangle with normals
      face_indices << 0
      vertices.reverse_each do |vertex|
        face_indices << get_vertex_index(vertex, all_vertices)
      end

      # convert to 1 based indices
      # face_indices.each_index {|i| face_indices[i] = face_indices[i] + 1}
    end

    data = GeometryData.new
    data.vertices = flatten_vertices(all_vertices)
    data.normals = []
    data.uvs = []
    data.faces = face_indices
    data.scale = 1
    data.visible = true
    data.castShadow = true
    data.receiveShadow = false
    data.doubleSided = true

    geometry = Geometry.new
    geometry.uuid = format_uuid(sub_surface.handle)
    geometry.type = 'Geometry'
    geometry.data = data.to_h
    geometries << geometry.to_h

    sub_surface_user_data = UserData.new
    sub_surface_user_data.handle = format_uuid(sub_surface.handle)
    sub_surface_user_data.name = sub_surface.name.to_s
    sub_surface_user_data.coincidentWithOutsideObject = false

    sub_surface_user_data.surfaceType = sub_surface.subSurfaceType
    if /Window/.match(sub_surface.subSurfaceType) || /Glass/.match(sub_surface.subSurfaceType) ||
       /Skylight/.match(sub_surface.subSurfaceType) || /TubularDaylight/.match(sub_surface.subSurfaceType)
      sub_surface_user_data.surfaceTypeMaterialName = 'Window'
    else
      sub_surface_user_data.surfaceTypeMaterialName = 'Door'
    end

    sub_surface_user_data.outsideBoundaryCondition = surface_user_data.outsideBoundaryCondition
    adjacent_sub_surface = sub_surface.adjacentSubSurface
    if adjacent_sub_surface.is_initialized
      sub_surface_user_data.outsideBoundaryConditionObjectName = adjacent_sub_surface.get.name.to_s
      sub_surface_user_data.outsideBoundaryConditionObjectHandle = format_uuid(adjacent_sub_surface.get.handle)

      other_site_transformation = OpenStudio::Transformation.new
      other_space = adjacent_sub_surface.get.space
      if !other_space.empty?
        other_site_transformation = building.transformation * other_space.get.transformation
      else
        other_site_transformation = building.transformation
      end

      other_vertices = other_site_transformation * adjacent_sub_surface.get.vertices
      if OpenStudio.circularEqual(site_transformation * sub_surface.vertices, OpenStudio.reverse(other_vertices))
        # puts "adjacent sub surfaces are coincident"
        surface_user_data.coincidentWithOutsideObject = true # controls display only, not energy model
      else
        # puts "adjacent sub surfaces are not coincident"
        surface_user_data.coincidentWithOutsideObject = false # controls display only, not energy model
      end

      sub_surface_user_data.boundaryMaterialName = 'Boundary_Surface'
    else
      if surface_user_data.boundaryMaterialName == 'Boundary_Surface'
        sub_surface_user_data.boundaryMaterialName = 'Undefined'
      else
        sub_surface_user_data.boundaryMaterialName = surface_user_data.boundaryMaterialName
      end
    end
    sub_surface_user_data.sunExposure = surface_user_data.sunExposure
    sub_surface_user_data.windExposure = surface_user_data.windExposure

    construction = sub_surface.construction
    if construction.is_initialized
      sub_surface_user_data.constructionName = construction.get.name.to_s
      sub_surface_user_data.constructionMaterialName = 'Construction_' + construction.get.name.to_s
    end
    sub_surface_user_data.spaceName = surface_user_data.spaceName
    sub_surface_user_data.thermalZoneName = surface_user_data.thermalZoneName
    sub_surface_user_data.thermalZoneMaterialName = surface_user_data.thermalZoneMaterialName
    sub_surface_user_data.spaceTypeName = surface_user_data.spaceTypeName
    sub_surface_user_data.spaceTypeMaterialName = surface_user_data.spaceTypeMaterialName
    sub_surface_user_data.buildingStoryName = surface_user_data.buildingStoryName
    sub_surface_user_data.buildingStoryMaterialName = surface_user_data.buildingStoryMaterialName

    # vertices = []
    # surface.vertices.each do |v|
    #  vertex = Vertex.new
    #  vertex.x = v.x
    #  vertex.y = v.y
    #  vertex.z = v.z
    #  vertices << vertex.to_h
    # end
    # sub_surface_user_data.vertices = vertices
    user_datas << sub_surface_user_data.to_h
  end

  return [geometries, user_datas]
end

.make_interior_partition_geometries(surface) ⇒ Object

turn an interior partition surface into geometries



718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
# File 'lib/measures/view_data/resources/va3c.rb', line 718

def self.make_interior_partition_geometries(surface)
  geometries = []
  user_datas = []

  # get the transformation to site coordinates
  site_transformation = OpenStudio::Transformation.new
  building = surface.model.getBuilding
  interior_partition_surface_group = surface.interiorPartitionSurfaceGroup

  space_name = nil
  thermal_zone_name = nil
  space_type_name = nil
  building_story_name = nil
  if !interior_partition_surface_group.empty?

    space = interior_partition_surface_group.get.space
    if space.is_initialized
      space = space.get
      space_name = space.name.to_s

      thermal_zone = space.thermalZone
      if thermal_zone.is_initialized
        thermal_zone_name = thermal_zone.get.name.to_s
      end

      space_type = space.spaceType
      if space_type.is_initialized
        space_type_name = space_type.get.name.to_s
      end

      building_story = space.buildingStory
      if building_story.is_initialized
        building_story_name = building_story.get.name.to_s
      end

      site_transformation = building.transformation * space.transformation * interior_partition_surface_group.get.transformation
    else
      site_transformation = building.transformation * interior_partition_surface_group.get.transformation
    end
  end

  # get the vertices
  surface_vertices = surface.vertices
  t = OpenStudio::Transformation.alignFace(surface_vertices)
  r = t.rotationMatrix
  tInv = t.inverse
  surface_vertices = OpenStudio.reverse(tInv * surface_vertices)

  # triangulate surface
  triangles = OpenStudio.computeTriangulation(surface_vertices, OpenStudio::Point3dVectorVector.new)
  if triangles.empty?
    puts "Failed to triangulate interior partition surface #{surface.name}"
    return geometries
  end

  all_vertices = []
  face_indices = []
  triangles.each do |vertices|
    vertices = site_transformation * t * vertices
    # normal = site_transformation.rotationMatrix*r*z

    # https://github.com/mrdoob/three.js/wiki/JSON-Model-format-3
    # 0 indicates triangle
    # 16 indicates triangle with normals
    face_indices << 0
    vertices.reverse_each do |vertex|
      face_indices << get_vertex_index(vertex, all_vertices)
    end

    # convert to 1 based indices
    # face_indices.each_index {|i| face_indices[i] = face_indices[i] + 1}
  end

  data = GeometryData.new
  data.vertices = flatten_vertices(all_vertices)
  data.normals = []
  data.uvs = []
  data.faces = face_indices
  data.scale = 1
  data.visible = true
  data.castShadow = true
  data.receiveShadow = false
  data.doubleSided = true

  geometry = Geometry.new
  geometry.uuid = format_uuid(surface.handle)
  geometry.type = 'Geometry'
  geometry.data = data.to_h
  geometries << geometry.to_h

  surface_user_data = UserData.new
  surface_user_data.handle = format_uuid(surface.handle)
  surface_user_data.name = surface.name.to_s
  surface_user_data.coincidentWithOutsideObject = false

  surface_user_data.surfaceType = 'InteriorPartitionSurface'
  surface_user_data.surfaceTypeMaterialName = 'InteriorPartitionSurface'

  # surface_user_data.outsideBoundaryCondition = nil
  # surface_user_data.outsideBoundaryConditionObjectName = nil
  # surface_user_data.outsideBoundaryConditionObjectHandle = nil
  surface_user_data.sunExposure = 'NoSun'
  surface_user_data.windExposure = 'NoWind'

  construction = surface.construction
  if construction.is_initialized
    surface_user_data.constructionName = construction.get.name.to_s
    surface_user_data.constructionMaterialName = 'Construction_' + construction.get.name.to_s
  end

  if space_name
    surface_user_data.spaceName = space_name
  end
  if thermal_zone_name
    surface_user_data.thermalZoneName = thermal_zone_name
    surface_user_data.thermalZoneMaterialName = 'ThermalZone_' + thermal_zone_name
  end
  if space_type_name
    surface_user_data.spaceTypeName = space_type_name
    surface_user_data.spaceTypeMaterialName = 'SpaceType_' + space_type_name
  end
  if building_story_name
    surface_user_data.buildingStoryName = building_story_name
    surface_user_data.buildingStoryMaterialName = 'BuildingStory_' + building_story_name
  end

  # vertices = []
  # surface.vertices.each do |v|
  #  vertex = Vertex.new
  #  vertex.x = v.x
  #  vertex.y = v.y
  #  vertex.z = v.z
  #  vertices << vertex.to_h
  # end
  # surface_user_data.vertices = vertices
  user_datas << surface_user_data.to_h

  return [geometries, user_datas]
end

.make_material(name, color, opacity, side, shininess = 50) ⇒ Object

create a material



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/measures/view_data/resources/va3c.rb', line 122

def self.make_material(name, color, opacity, side, shininess = 50)
  transparent = false
  if opacity < 1
    transparent = true
  end

  material = { uuid: format_uuid(OpenStudio.createUUID).to_s,
               name: name,
               type: 'MeshPhongMaterial',
               color: color.to_s.hex,
               ambient: color.to_s.hex,
               emissive: '0x000000'.hex,
               specular: color.to_s.hex,
               shininess: shininess,
               opacity: opacity,
               transparent: transparent,
               wireframe: false,
               side: side }
  return material
end

.make_shade_geometries(surface) ⇒ Object

turn a shading surface into geometries



572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
# File 'lib/measures/view_data/resources/va3c.rb', line 572

def self.make_shade_geometries(surface)
  geometries = []
  user_datas = []

  # get the transformation to site coordinates
  site_transformation = OpenStudio::Transformation.new
  building = surface.model.getBuilding

  shading_surface_group = surface.shadingSurfaceGroup
  shading_surface_type = 'Building'
  space_name = nil
  thermal_zone_name = nil
  space_type_name = nil
  building_story_name = nil
  if !shading_surface_group.empty?
    shading_surface_type = shading_surface_group.get.shadingSurfaceType

    space = shading_surface_group.get.space
    if space.is_initialized
      space = space.get
      space_name = space.name.to_s

      thermal_zone = space.thermalZone
      if thermal_zone.is_initialized
        thermal_zone_name = thermal_zone.get.name.to_s
      end

      space_type = space.spaceType
      if space_type.is_initialized
        space_type_name = space_type.get.name.to_s
      end

      building_story = space.buildingStory
      if building_story.is_initialized
        building_story_name = building_story.get.name.to_s
      end

      site_transformation = building.transformation * space.transformation * shading_surface_group.get.transformation
    elsif /Site/i.match(shading_surface_type)
      site_transformation = shading_surface_group.get.transformation
    else
      site_transformation = building.transformation * shading_surface_group.get.transformation
    end

  end

  # get the vertices
  surface_vertices = surface.vertices
  t = OpenStudio::Transformation.alignFace(surface_vertices)
  r = t.rotationMatrix
  tInv = t.inverse
  surface_vertices = OpenStudio.reverse(tInv * surface_vertices)

  # triangulate surface
  triangles = OpenStudio.computeTriangulation(surface_vertices, OpenStudio::Point3dVectorVector.new)
  if triangles.empty?
    puts "Failed to triangulate shading surface #{surface.name}"
    return geometries
  end

  all_vertices = []
  face_indices = []
  triangles.each do |vertices|
    vertices = site_transformation * t * vertices
    # normal = site_transformation.rotationMatrix*r*z

    # https://github.com/mrdoob/three.js/wiki/JSON-Model-format-3
    # 0 indicates triangle
    # 16 indicates triangle with normals
    face_indices << 0
    vertices.reverse_each do |vertex|
      face_indices << get_vertex_index(vertex, all_vertices)
    end

    # convert to 1 based indices
    # face_indices.each_index {|i| face_indices[i] = face_indices[i] + 1}
  end

  data = GeometryData.new
  data.vertices = flatten_vertices(all_vertices)
  data.normals = []
  data.uvs = []
  data.faces = face_indices
  data.scale = 1
  data.visible = true
  data.castShadow = true
  data.receiveShadow = false
  data.doubleSided = true

  geometry = Geometry.new
  geometry.uuid = format_uuid(surface.handle)
  geometry.type = 'Geometry'
  geometry.data = data.to_h
  geometries << geometry.to_h

  surface_user_data = UserData.new
  surface_user_data.handle = format_uuid(surface.handle)
  surface_user_data.name = surface.name.to_s
  surface_user_data.coincidentWithOutsideObject = false

  surface_user_data.surfaceType = shading_surface_type + 'Shading'
  surface_user_data.surfaceTypeMaterialName = shading_surface_type + 'Shading'

  # surface_user_data.outsideBoundaryCondition = nil
  # surface_user_data.outsideBoundaryConditionObjectName = nil
  # surface_user_data.outsideBoundaryConditionObjectHandle = nil
  surface_user_data.sunExposure = 'SunExposed'
  surface_user_data.windExposure = 'WindExposed'

  construction = surface.construction
  if construction.is_initialized
    surface_user_data.constructionName = construction.get.name.to_s
    surface_user_data.constructionMaterialName = 'Construction_' + construction.get.name.to_s
  end

  if space_name
    surface_user_data.spaceName = space_name
  end
  if thermal_zone_name
    surface_user_data.thermalZoneName = thermal_zone_name
    surface_user_data.thermalZoneMaterialName = 'ThermalZone_' + thermal_zone_name
  end
  if space_type_name
    surface_user_data.spaceTypeName = space_type_name
    surface_user_data.spaceTypeMaterialName = 'SpaceType_' + space_type_name
  end
  if building_story_name
    surface_user_data.buildingStoryName = building_story_name
    surface_user_data.buildingStoryMaterialName = 'BuildingStory_' + building_story_name
  end

  # vertices = []
  # surface.vertices.each do |v|
  #  vertex = Vertex.new
  #  vertex.x = v.x
  #  vertex.y = v.y
  #  vertex.z = v.z
  #  vertices << vertex.to_h
  # end
  # surface_user_data.vertices = vertices
  user_datas << surface_user_data.to_h

  return [geometries, user_datas]
end