Module: Floorplanner::ColladaExport

Included in:
Design
Defined in:
lib/floorplanner/collada_export.rb

Constant Summary collapse

CACHE_PATH =
File.join(Floorplanner.config['dae_cache_path'], 'textures_2d')

Instance Method Summary collapse

Instance Method Details

#assetsObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/floorplanner/collada_export.rb', line 42

def assets
  return @assets if @assets
  @assets = {}
  @doc.assets.each do |asset|
    next unless asset.url3d && !asset.url3d.empty?

    # TODO: store asset bounding box
    dae = Floorplanner::DAE.get(asset)
    next unless dae
    @assets.store(asset.id, dae)
  end
  @assets
end

#objectsObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/floorplanner/collada_export.rb', line 56

def objects
  result = []
  @doc.objects.each do |item|
      next unless assets[item.asset]
      asset = assets[item.asset]

      pos = item.position
      rot = item.rotation || Geom::Number3D.new
      scale = asset.scale_ratio(item.size)

      if item.mirrored
        m_mirror = Geom::Matrix3D.reflection(
          Geom::Plane.new(item.mirrored, Geom::Number3D.new))
      end

      m_scale     = Geom::Matrix3D.scale(scale.x, scale.y, scale.z)
      m_rotate    = Geom::Matrix3D.rotation(0, 0, 1, (Math::PI/180)*rot.z)
      m_translate = Geom::Matrix3D.translation(pos.x, pos.y, pos.z)

      m_combined  = m_rotate * m_scale
      m_combined  = m_mirror * m_combined if m_mirror
      m_combined  = m_translate * m_combined

      result << {
        :asset  => asset,
        :matrix => m_combined,
        :color  => Floorplanner.read_color(item.color)
      }
  end
  result
end

#save_textures(root_path) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/floorplanner/collada_export.rb', line 88

def save_textures root_path
  img_path = File.join(root_path, 'textures')
  FileUtils.mkdir_p img_path
  assets.each_value do |asset|
    asset.save_textures img_path
  end

  t2d_path = File.join(img_path, 'textures_2d')
  FileUtils.mkdir_p t2d_path
  @areas.each do |area|
    next unless area.data[:texture]
    texture_url = area.data[:texture]
    fn = texture_url.match(/.*\/(.*)/)[1]
    tex_path = File.join(t2d_path, fn)
    area.data[:texture] = File.join('textures', 'textures_2d', fn)
    next if File.exists?(tex_path)

    FileUtils.mkdir_p CACHE_PATH
    cached_path = File.join(CACHE_PATH, fn)
    unless File.exists?(cached_path)
      puts "Downloading texture: %s" % texture_url
      begin
        cached = File.new(cached_path, 'w')
        remote = open(Floorplanner.config['content_base_url'] + 
          URI.escape(texture_url))
        cached.write(remote.read)
        cached.close
      rescue
        $stderr.puts "Error downloading texture: %s" % fn
      end
    end
    FileUtils.cp(cached_path, tex_path)
  end
end

#to_dae(conf) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/floorplanner/collada_export.rb', line 26

def to_dae conf
  raise "No geometries to export" unless @areas && @walls
  @assets   = assets
  @elements = objects

  # somehow...
  @walls.reverse
  @areas.each {|a| a.reverse}
  @conf = conf

  template = ERB.new(
    File.read(
      File.join(File.dirname(__FILE__), '..', '..', 'views', 'design.dae.erb')))
  template.result(binding)
end