#######################################################################
#
# $Id: GroupGeometryObjects.py,v 1.2 2008-02-07 05:45:25 lorenz Exp $
# 
# Copyright (C) 2004-2008 Petr Lorenz
# All rights reserved.
# 
# ##### BEGIN LICENSE BLOCK #####
#
# Version: MPL 1.1 / GPL 2.0 / LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
# 
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
# 
# The Original Code is Petr Lorenz emGine Graphical User Interface (GUI).
# 
# The Initial Developer of the Original Code is Petr Lorenz.
# Portions created by the Initial Developer are Copyright (C) 2004-2008.
# All Rights Reserved.
# 
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the  "GPL"), or the
# GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
# case the provisions of the GPL or the LGPL are applicable instead of those
# above.  If you wish to allow use of your version of this file only under the
# terms of either the GPL or the LGPL and not to allow others to use your
# version of this file under the MPL, indicate your decision by deleting the
# provisions above and replace them with the notice and other provisions
# required by the GPL or LGPL.  If you do not delete the provisions above, a
# recipient may use your version of this file under the terms of any one of the
# MPL License, the GPL or the LGPL.
#
# ##### END LICENSE BLOCK #####
#
#######################################################################
# 
# $Log: GroupGeometryObjects.py,v $
# Revision 1.2  2008-02-07 05:45:25  lorenz
# * Fixed 'Simulation Boundary View' check problem
#   when closing a project.
#
# * Fixed Bounding Box problems when moving/editing a geometry.
#
# Revision 1.1.1.1  2008-01-28 06:15:35  lorenz
# Import emGine GUI v. 0.7.1
#
# Revision 1.3  2007-07-02 06:37:18  lorenz
# Clean up of old code.
#
# Revision 1.2  2007-05-18 16:37:01  lorenz
# Work on 'surrounding space' simbox and
#  'emgine_gui' tag
#
# Revision 1.1.1.1  2007-05-08 06:22:35  lorenz
# Initial Import
#
#######################################################################

# July 2006 - January 2008

# TODO: - Override addition and multiplication with boolean operations
#       - add transformations

from Group import Group

from GeometryObject import *

class GroupGeometryObjects(Group):
  def __init__(self):
    Group.__init__(self, "GroupGeometryObjects")
    self.bbox = None

  def update_bbox(self):
    return
    # FIXME: The routine below is buggy
    #        The assignment self.bbox = go.bbox is changing the boundary box
    #        of the go object also, since not a deep copy !!!!
    go = self.group_list[-1]       # last object added
    if not self.bbox:
      self.bbox = go.bbox        # only one object in the list -> copy bbox # FIXME: This is a bug
    else:
      if go.bbox[0][0] < self.bbox[0][0]:
        self.bbox[0][0] = go.bbox[0][0]
      if go.bbox[0][1] < self.bbox[0][1]:
        self.bbox[0][1] = go.bbox[0][1]
      if go.bbox[0][2] < self.bbox[0][2]:
        self.bbox[0][2] = go.bbox[0][2]

      if go.bbox[1][0] > self.bbox[1][0]:
        self.bbox[1][0] = go.bbox[1][0]
      if go.bbox[1][1] > self.bbox[1][1]:
        self.bbox[1][1] = go.bbox[1][1]
      if go.bbox[1][2] > self.bbox[1][2]:
        self.bbox[1][2] = go.bbox[1][2]

  def add(self, geom_object):
    if not isinstance(geom_object, GeometryObject):
      return

    Group.add(self, geom_object)
    self.update_bbox()

  # Description:
  # remove the object with id from the Group
  def remove(self, name):
    #print self.group_list
    go = self.get_name(name)
    if go:
      #print "**** Removing ", go
      self.group_list.remove(go)
    #print self.group_list

  # Description:
  # get the object with id from the Group
  def get_name(self, name):
    for go in self.group_list:
      if go.name == name:
	return go
    return None

  def get_discretization_points(self):
    dpoints = list()
    for gobject in self.group_list:
      #print ">> Name of gobject: ", gobject.name
      #print "   gobject dpoints: ", gobject.get_discretization_points()
      for dpoint in gobject.get_discretization_points():
        dpoints.append(dpoint)
    return dpoints
      

###############################################
if __name__ == '__main__':
  from GeometryObjectPatch import *

  print "Testing GroupGeometryObjects Module..."

  group = GroupGeometryObjects()
  patch1 = GeometryObjectPatch("Patch1", [0,0,0], [1,1,0])
  patch2 = GeometryObjectPatch("Patch2", [-1,-1,-1], [0.5,0.5,-1])
  group.add(patch1)
  group.add(patch2)
  print group.get_discretization_points()
