# coding: utf-8
import ROOT
from ROOT import gROOT, gSystem, TGeoManager
from ROOT import TGeoTube, TGeoVolume, TGeoMaterial, TGeoMedium, TGeoTranslation, TGeoCompositeShape
from ROOT import TGeoIntersection, TGeoXtru, TGeoBBox, gPad
from array import array

"""
ROOT test case.
Clip using TGeoCompositeShape
"""

gROOT.Reset()

########################################################**MANAGER**####################################

mgr = TGeoManager("manager", "")

########################################################**MATERIALS**##################################

vacuum_mat = TGeoMaterial("vacuum", 0, 0, 0)
vacuum = TGeoMedium ("vacuum", 1, vacuum_mat)

steal_mat = TGeoMaterial("steal", 0, 0, 0)
steal = TGeoMedium ("steal", 4, steal_mat)

########################################################**WORLD**######################################

world = mgr.MakeBox("world", vacuum, 200, 200, 200)
world.SetTransparency(100)

########################################################**GEOMETRY**###################################

cyl = TGeoTube("tube", 0, 12, 100)
cylV = TGeoVolume ("tubeV", cyl, steal)
cylV.SetLineColor (ROOT.kGray)

singleBox = TGeoBBox ("singleBox", 50, 50, 100)
singleBoxV = TGeoVolume ("singleBoxV", singleBox, steal)
singleBoxV.SetLineColor (ROOT.kOrange)

########################################################**CLIPPING SHAPES**############################

# 1/4 clip shape description

clipS = TGeoBBox ("clip", 50, 50, 102)

t5 = TGeoTranslation ("t5", 50, 50, 0)
t5.RegisterYourself()

# 1/8 clip shape description

clip8S = TGeoXtru (2)
x = array('d')
x.append(0.)
x.append(50.)
x.append(50.)

y = array('d')
y.append(0.)
y.append(0.)
y.append(50.)

clip8S.DefinePolygon (3, x, y)
clip8S.DefineSection (0, -105, 0, 0, 1)
clip8S.DefineSection (1, 105, 0, 0, 1)

# Volume description just to print it
clip8V = TGeoVolume ("clip8V", clip8S, steal)


########################################################**CLIPPING OPERATIONS**########################

##PARAMETERS

"""
Uncomment the case you want to test
"""

#Cylinder 1/4
geoToClip = cylV
clippingShape = clipS
translationMatrix = t5
clipValue = 0.25 #for the result computing

#Cylinder 1/8
# geoToClip = cylV
# clippingShape = clip8S
# translationMatrix = 0
# clipValue = 0.125 

#Cube 1/4
# geoToClip = singleBoxV
# clippingShape = clipS
# translationMatrix = t5
# clipValue = 0.25

#Cube 1/8
# geoToClip = singleBoxV
# clippingShape = clip8S
# translationMatrix = 0
# clipValue = 0.125


#The intersection between the geometry and the cliping shape
intersect = TGeoIntersection (geoToClip.GetShape(), clippingShape, 0, translationMatrix)
clipped = TGeoCompositeShape ("clip", intersect)

clippedV = TGeoVolume ("clipped", clipped, steal)
clippedV.SetLineColor(ROOT.kRed)

#Control
print "Control by getting the capacity of the geometries before and after the clip : "
print geoToClip.GetName()+" capacity > " + str(geoToClip.Capacity())
print "Result      > " + str(clipped.Capacity())
print "Expected    > " + str(geoToClip.Capacity()*clipValue)
print "Difference  > " + str(geoToClip.Capacity()*clipValue - clipped.Capacity())

world.AddNode(clippedV, 0)

# The full geometry in transparency
geoToClip.SetLineColor (ROOT.kOrange)
geoToClip.SetTransparency (70)
world.AddNode(geoToClip,0)

########################################################**DISPLAY**####################################

mgr.SetTopVolume(world)

mgr.GetMasterVolume().SetVisContainers(ROOT.kTRUE)
mgr.SetNsegments(100)

mgr.SetTopVisible(ROOT.kTRUE)
mgr.SetVisLevel(15)
mgr.CloseGeometry()

mgr.GetMasterVolume().Draw()
# gPad.GetView().ShowAxis()

while True:
    pass

