def fromOriginAndSize(cls, origin, width, length, height, convertToMeters=1, nDivXYZ=None, grading=None, xAxis=None): """Create BlockMeshDict from BFBlockGeometries. Args: origin: Minimum point of bounding box as (x, y, z). width: Width in x direction. length: Length in y direction. height: Height in y direction. convertToMeters: Scaling factor for the vertex coordinates. nDivXYZ: Number of divisions in (x, y, z) as a tuple (default: 5, 5, 5). grading: A simpleGrading (default: simpleGrading(1, 1, 1)). xAxis: An optional tuple that indicates the xAxis direction (default: (1, 0)). """ _xAxis = vectormath.normalize((xAxis[0], xAxis[1], 0) if xAxis else (1, 0, 0)) _zAxis = (0, 0, 1) _yAxis = vectormath.crossProduct(_zAxis, _xAxis) vertices = tuple( vectormath.move(origin, vectormath.sums((vectormath.scale(_xAxis, i * width), vectormath.scale(_yAxis, j * length), vectormath.scale(_zAxis, k * height)) )) for i in range(2) for j in range(2) for k in range(2)) return cls.fromVertices(vertices, convertToMeters, nDivXYZ, grading, xAxis)
def __calculate2dPoints(v, o, n, w): # project point p = vectormath.project(v, o, n) # move the projected point backwards for half of the width t = vectormath.scale(vectormath.normalize(vectormath.subtract(v, p)), w / 2.0) return vectormath.move(p, t)
def fromMinMax(cls, minPt, maxPt, convertToMeters=1, nDivXYZ=None, grading=None, xAxis=None): """Create BlockMeshDict from minimum and maximum point. Args: minPt: Minimum point of bounding box as (x, y, z). maxPt: Maximum point of bounding box as (x, y, z). convertToMeters: Scaling factor for the vertex coordinates. nDivXYZ: Number of divisions in (x, y, z) as a tuple (default: 5, 5, 5). grading: A simpleGrading (default: simpleGrading(1, 1, 1)). xAxis: An optional tuple that indicates the xAxis direction (default: (1, 0)). """ _xAxis = vectormath.normalize((xAxis[0], xAxis[1], 0) if xAxis else (1, 0, 0)) _zAxis = (0, 0, 1) _yAxis = vectormath.crossProduct(_zAxis, _xAxis) diagonal2D = tuple(i - j for i, j in zip(maxPt, minPt))[:2] _angle = radians(vectormath.angleAnitclockwise(_xAxis[:2], diagonal2D)) width = cos(_angle) * vectormath.length(diagonal2D) length = sin(_angle) * vectormath.length(diagonal2D) height = maxPt[2] - minPt[2] vertices = tuple( vectormath.move(minPt, vectormath.sums((vectormath.scale(_xAxis, i * width), vectormath.scale(_yAxis, j * length), vectormath.scale(_zAxis, k * height)) )) for i in range(2) for j in range(2) for k in range(2)) return cls.fromVertices(vertices, convertToMeters, nDivXYZ, grading, xAxis)
def getMovement(self): dx, dy, dz, rotationAngle = (0, 0, 0, 0) #move to ball/square border intersection currentPosition = self.body.getPosition() target = self.target.getPosition() if (target[0]-currentPosition[0]) * (target[0]-currentPosition[0]) + \ (target[2]-currentPosition[2]) * (target[2]-currentPosition[2]) < 2.0 and \ currentPosition[1]<1.1: dy = 2.0 if vectormath.isPointInSquare(target, self.min, self.max): targetPosition = target else: targetPosition = vectormath.getBallAndSquareIntersection( target, self.target.getLinearVel(), self.min, self.max) targetPosition = (targetPosition[0] + random() - 0.5, targetPosition[1], targetPosition[2] + random() - 0.5) vectorAB = vectormath.getVector(currentPosition, targetPosition) #to make oponents run to ball coords - self length vABAngle = atan2(vectorAB[2], vectorAB[0]) vABMagnitude = vectormath.getMagnitudeV(vectorAB) newVABMagnitude = vABMagnitude - 2.0 #malas garums/2 newVABX = [ cos(vABAngle) * newVABMagnitude, vectorAB[1], sin(vABAngle) * newVABMagnitude ] vectorAB = newVABX if vABMagnitude < 1.8: moveSpeed = 700 * self.body.getMass().mass else: moveSpeed = 4000 * self.body.getMass().mass currentRotationTuple = self.body.getRotation() currentRotation = currentRotationTuple[2], currentRotationTuple[ 5], currentRotationTuple[8] vectorAB = vectormath.normalize(vectorAB[0], 0, vectorAB[2]) rotationAngle = vectormath.getAngleBetweenVectors( currentRotation, vectorAB) currentRotation = vectormath.negate(currentRotation) dx = currentRotation[0] dz = currentRotation[2] #/------------------ return dx * moveSpeed, dy * moveSpeed, dz * moveSpeed, rotationAngle * moveSpeed
def x_axis(self): """X axis as a tuple.""" if self._x_axis: return self._x_axis else: self._x_axis = vectormath.normalize( vectormath.subtract(self.vertices[1], self.vertices[0])) return self._x_axis
def getMovement(self): dx, dy, dz, rotationAngle = (0,0,0,0) #move to ball/square border intersection currentPosition = self.body.getPosition() target = self.target.getPosition() if (target[0]-currentPosition[0]) * (target[0]-currentPosition[0]) + \ (target[2]-currentPosition[2]) * (target[2]-currentPosition[2]) < 2.0 and \ currentPosition[1]<1.1: dy = 2.0 if vectormath.isPointInSquare(target, self.min, self.max): targetPosition = target else: targetPosition = vectormath.getBallAndSquareIntersection(target, self.target.getLinearVel(), self.min, self.max) targetPosition = (targetPosition[0] + random() - 0.5, targetPosition[1], targetPosition[2] + random() - 0.5) vectorAB = vectormath.getVector(currentPosition, targetPosition) #to make oponents run to ball coords - self length vABAngle = atan2(vectorAB[2], vectorAB[0]) vABMagnitude = vectormath.getMagnitudeV(vectorAB) newVABMagnitude = vABMagnitude - 2.0 #malas garums/2 newVABX = [cos(vABAngle) * newVABMagnitude, vectorAB[1], sin(vABAngle) * newVABMagnitude] vectorAB = newVABX if vABMagnitude<1.8: moveSpeed = 700 * self.body.getMass().mass else: moveSpeed = 4000 * self.body.getMass().mass currentRotationTuple = self.body.getRotation() currentRotation = currentRotationTuple[2], currentRotationTuple[5], currentRotationTuple[8] vectorAB = vectormath.normalize(vectorAB[0], 0, vectorAB[2]) rotationAngle = vectormath.getAngleBetweenVectors(currentRotation, vectorAB) currentRotation = vectormath.negate(currentRotation) dx = currentRotation[0] dz = currentRotation[2] #/------------------ return dx * moveSpeed, dy * moveSpeed, dz * moveSpeed, rotationAngle * moveSpeed
def getPosition(avatar, pos, bone1, bone2, state, limit): #figure out which state we are in, and change the bone we are rotating dir = array(pos) - array(avatar.getBone(bone1).getPosition(viz.ABS_GLOBAL)); #if the hands are swapped if(STATE == 1): oriPosition = array(avatar.getBone(bone2).getPosition(viz.ABS_GLOBAL)); else: oriPosition = array(avatar.getBone(bone1).getPosition(viz.ABS_GLOBAL)); dir_len = dir.dot(dir); dir_len = math.sqrt(dir_len); #normalize the direction given by ppt dir_norm = vectormath.normalize(dir); posArray = oriPosition + dir; pos = [posArray[0], posArray[1], posArray[2]]; #print "state: " + str(state); #if range of arms are limited if(state == 1): a = dir; #print "dir: " + str(dir); #a = array([-1, 1, 1]); a_len = a.dot(a); a_len = math.sqrt(a_len); #doing spherical coordinates, getting the radius first r = a_len; theta = math.acos(-a[1] / r); phi = math.atan2(a[0], a[2]); #print "theta: " + str(theta * 180 / math.pi); #print "phi: " + str(phi * 180 / math.pi); theta = theta * limit; #print "new theta: " + str(theta); y = -r * math.cos(theta); x = r * math.sin(phi) * math.sin(theta); z = r * math.cos(phi) * math.sin(theta); newDir = array([x, y, z]); pos = oriPosition + newDir; #print "newPos: " + str(pos); return pos;
def make2d(self, planeOrigin, planeNormal, width=0.1): """Make the blockMeshDict two dimensional. Args: planeOrigin: Plane origin as (x, y, z). planeNormal: Plane normal as (x, y, z). width: width of 2d blockMeshDict (default: 01). """ # copy original vertices if not self.__original3dVertices: self.__original3dVertices = self.vertices else: # load original 3d vertices self.make3d() n = vectormath.normalize(planeNormal) # project all vertices to plane and move them in direction of normal # by half of width self.__vertices = [ self.__calculate2dPoints(v, planeOrigin, n, width) for v in self.vertices ] # set boundary condition to empty # and number of divisions to 1 in shortest side minimum = min(self.width, self.length, self.height) if self.width == minimum: self.nDivXYZ = (1, self.nDivXYZ[1], self.nDivXYZ[2]) self.__is2dInXDir = True # set both sides to empty self.__setBoundaryToEmpty(4) self.__setBoundaryToEmpty(5) elif self.length == minimum: self.nDivXYZ = (self.nDivXYZ[0], 1, self.nDivXYZ[2]) self.__is2dInYDir = True # set inlet and outlet to empty self.__setBoundaryToEmpty(0) self.__setBoundaryToEmpty(1) elif self.height == minimum: self.nDivXYZ = (self.nDivXYZ[0], self.nDivXYZ[1], 1) self.__is2dInZDir = True # set top and bottom to empty self.__setBoundaryToEmpty(2) self.__setBoundaryToEmpty(3)
def from_min_max(cls, min_pt, max_pt, convertToMeters=1, n_div_xyz=None, grading=None, x_axis=None): """Create BlockMeshDict from minimum and maximum point. Args: min_pt: Minimum point of bounding box as (x, y, z). max_pt: Maximum point of bounding box as (x, y, z). convertToMeters: Scaling factor for the vertex coordinates. n_div_xyz: Number of divisions in (x, y, z) as a tuple (default: 5, 5, 5). grading: A simpleGrading (default: simpleGrading(1, 1, 1)). x_axis: An optional tuple that indicates the x_axis direction (default: (1, 0)). """ _x_axis = vectormath.normalize((x_axis[0], x_axis[1], 0) if x_axis else (1, 0, 0)) _z_axis = (0, 0, 1) _y_axis = vectormath.cross_product(_z_axis, _x_axis) diagonal2_d = tuple(i - j for i, j in zip(max_pt, min_pt))[:2] _angle = radians( vectormath.angle_anitclockwise(_x_axis[:2], diagonal2_d)) width = cos(_angle) * vectormath.length(diagonal2_d) length = sin(_angle) * vectormath.length(diagonal2_d) height = max_pt[2] - min_pt[2] vertices = [ vectormath.move( min_pt, vectormath.sums((vectormath.scale(_x_axis, i * width), vectormath.scale(_y_axis, j * length), vectormath.scale(_z_axis, k * height)))) for i in range(2) for j in range(2) for k in range(2) ] return cls.from_vertices(vertices, convertToMeters, n_div_xyz, grading, x_axis)
def fromOriginAndSize(cls, origin, width, length, height, convertToMeters=1, nDivXYZ=None, grading=None, xAxis=None): """Create BlockMeshDict from BFBlockGeometries. Args: origin: Minimum point of bounding box as (x, y, z). width: Width in x direction. length: Length in y direction. height: Height in y direction. convertToMeters: Scaling factor for the vertex coordinates. nDivXYZ: Number of divisions in (x, y, z) as a tuple (default: 5, 5, 5). grading: A simpleGrading (default: simpleGrading(1, 1, 1)). xAxis: An optional tuple that indicates the xAxis direction (default: (1, 0)). """ _xAxis = vectormath.normalize((xAxis[0], xAxis[1], 0) if xAxis else (1, 0, 0)) _zAxis = (0, 0, 1) _yAxis = vectormath.crossProduct(_zAxis, _xAxis) vertices = [ vectormath.move( origin, vectormath.sums((vectormath.scale(_xAxis, i * width), vectormath.scale(_yAxis, j * length), vectormath.scale(_zAxis, k * height)))) for i in range(2) for j in range(2) for k in range(2) ] return cls.fromVertices(vertices, convertToMeters, nDivXYZ, grading, xAxis)
def make2d(self, planeOrigin, planeNormal, width=0.1): """Create a new 2D blockMeshDict from this blockMeshDict. Args: planeOrigin: Plane origin as (x, y, z). planeNormal: Plane normal as (x, y, z). width: width of 2d blockMeshDict (default: 01). """ # duplicate blockMeshDict bmd = self.duplicate() n = vectormath.normalize(planeNormal) # project all vertices to plane and move them in direction of normal # by half of width bmd.__vertices = tuple( self.__calculate2dPoints(v, planeOrigin, n, width) for v in bmd.vertices) # set boundary condition to empty # and number of divisions to 1 in shortest side minimum = min(bmd.width, bmd.length, bmd.height) if bmd.width == minimum: bmd.nDivXYZ = (1, bmd.nDivXYZ[1], bmd.nDivXYZ[2]) # set both sides to empty elif bmd.length == minimum: bmd.nDivXYZ = (bmd.nDivXYZ[0], 1, bmd.nDivXYZ[2]) # set inlet and outlet to empty # bmd.inlet.boundaryCondition = Empty() elif bmd.height == minimum: bmd.nDivXYZ = (bmd.nDivXYZ[0], bmd.nDivXYZ[1], 1) # set top and bottom to empty print('WARNING: make2d doesn\'t update boundary conditions to Empty.') return bmd
def fromGeometriesWindVectorAndParameters(cls, name, geometries, windVector, tunnelParameters, roughness, meshingParameters=None, Zref=None, convertToMeters=1): """Create a windTunnel based on size, wind speed and wind direction.""" # butterfly geometries geos = tuple(cls.__checkInputGeometry(geo) for geo in geometries) # update boundary condition of wall geometries for bfGeometry in geometries: bfGeometry.boundaryCondition = WindTunnelWallBoundaryCondition() tp = tunnelParameters # find xAxis # project wind vector to XY Plane windVector = (windVector[0], windVector[1], 0) zAxis = (0, 0, 1) xAxis = vm.crossProduct(windVector, zAxis) yAxis = vm.normalize(windVector) # get size of bounding box from blockMeshDict minPt, maxPt = calculateMinMaxFromBFGeometries(geos, xAxis) _blockMeshDict = BlockMeshDict.fromMinMax(minPt, maxPt, convertToMeters, xAxis=xAxis) # scale based on wind tunnel parameters ver = _blockMeshDict.vertices height = _blockMeshDict.height v0 = vm.move(ver[0], vm.scale(yAxis, -tp.windward * height)) v0 = vm.move(v0, vm.scale(xAxis, -tp.side * height)) v1 = vm.move(ver[1], vm.scale(yAxis, -tp.windward * height)) v1 = vm.move(v1, vm.scale(xAxis, tp.side * height)) v2 = vm.move(ver[2], vm.scale(yAxis, tp.leeward * height)) v2 = vm.move(v2, vm.scale(xAxis, tp.side * height)) v3 = vm.move(ver[3], vm.scale(yAxis, tp.leeward * height)) v3 = vm.move(v3, vm.scale(xAxis, -tp.side * height)) v4 = vm.move(v0, vm.scale(zAxis, tp.top * height)) v5 = vm.move(v1, vm.scale(zAxis, tp.top * height)) v6 = vm.move(v2, vm.scale(zAxis, tp.top * height)) v7 = vm.move(v3, vm.scale(zAxis, tp.top * height)) # create inlet, outlet, etc ablConditions = ABLConditions.fromInputValues( flowSpeed=vm.length(windVector), z0=roughness, flowDir=vm.normalize(windVector), zGround=_blockMeshDict.minZ) _order = (range(4), ) inlet = BFBlockGeometry( 'inlet', (v0, v1, v5, v4), _order, ((v0, v1, v5, v4), ), WindTunnelInletBoundaryCondition(ablConditions)) outlet = BFBlockGeometry('outlet', (v2, v3, v7, v6), _order, ((v2, v3, v7, v6), ), WindTunnelOutletBoundaryCondition()) rightSide = BFBlockGeometry('rightSide', (v1, v2, v6, v5), _order, ((v1, v2, v6, v5), ), WindTunnelTopAndSidesBoundaryCondition()) leftSide = BFBlockGeometry('leftSide', (v3, v0, v4, v7), _order, ((v3, v0, v4, v7), ), WindTunnelTopAndSidesBoundaryCondition()) top = BFBlockGeometry('top', (v4, v5, v6, v7), _order, ((v4, v5, v6, v7), ), WindTunnelTopAndSidesBoundaryCondition()) ground = BFBlockGeometry( 'ground', (v3, v2, v1, v0), (range(4), ), ((v3, v2, v1, v0), ), WindTunnelGroundBoundaryCondition(ablConditions)) # return the class wt = cls(name, inlet, outlet, (rightSide, leftSide), top, ground, geometries, roughness, meshingParameters, Zref, convertToMeters) return wt
def fromGeometriesWindVectorAndParameters( cls, name, geometries, windVector, tunnelParameters, roughness, meshingParameters=None, Zref=None, convertToMeters=1): """Create a windTunnel based on size, wind speed and wind direction.""" # butterfly geometries geos = tuple(cls.__checkInputGeometry(geo) for geo in geometries) # update boundary condition of wall geometries for bfGeometry in geometries: bfGeometry.boundaryCondition = WindTunnelWallBoundaryCondition( bfGeometry.boundaryCondition.refLevels ) tp = tunnelParameters # find xAxis # project wind vector to XY Plane windVector = (windVector[0], windVector[1], 0) zAxis = (0, 0, 1) xAxis = vm.crossProduct(windVector, zAxis) yAxis = vm.normalize(windVector) # get size of bounding box from blockMeshDict minPt, maxPt = calculateMinMaxFromBFGeometries(geos, xAxis) _blockMeshDict = BlockMeshDict.fromMinMax(minPt, maxPt, convertToMeters, xAxis=xAxis) # scale based on wind tunnel parameters ver = _blockMeshDict.vertices height = _blockMeshDict.height v0 = vm.move(ver[0], vm.scale(yAxis, -tp.windward * height)) v0 = vm.move(v0, vm.scale(xAxis, -tp.side * height)) v1 = vm.move(ver[1], vm.scale(yAxis, -tp.windward * height)) v1 = vm.move(v1, vm.scale(xAxis, tp.side * height)) v2 = vm.move(ver[2], vm.scale(yAxis, tp.leeward * height)) v2 = vm.move(v2, vm.scale(xAxis, tp.side * height)) v3 = vm.move(ver[3], vm.scale(yAxis, tp.leeward * height)) v3 = vm.move(v3, vm.scale(xAxis, -tp.side * height)) v4 = vm.move(v0, vm.scale(zAxis, tp.top * height)) v5 = vm.move(v1, vm.scale(zAxis, tp.top * height)) v6 = vm.move(v2, vm.scale(zAxis, tp.top * height)) v7 = vm.move(v3, vm.scale(zAxis, tp.top * height)) # create inlet, outlet, etc ablConditions = ABLConditions.fromInputValues( flowSpeed=vm.length(windVector), z0=roughness, flowDir=vm.normalize(windVector), zGround=_blockMeshDict.minZ) _order = (range(4),) inlet = BFBlockGeometry( 'inlet', (v0, v1, v5, v4), _order, ((v0, v1, v5, v4),), WindTunnelInletBoundaryCondition(ablConditions)) outlet = BFBlockGeometry( 'outlet', (v2, v3, v7, v6), _order, ((v2, v3, v7, v6),), WindTunnelOutletBoundaryCondition()) rightSide = BFBlockGeometry( 'rightSide', (v1, v2, v6, v5), _order, ((v1, v2, v6, v5),), WindTunnelTopAndSidesBoundaryCondition()) leftSide = BFBlockGeometry( 'leftSide', (v3, v0, v4, v7), _order, ((v3, v0, v4, v7),), WindTunnelTopAndSidesBoundaryCondition()) top = BFBlockGeometry('top', (v4, v5, v6, v7), _order, ((v4, v5, v6, v7),), WindTunnelTopAndSidesBoundaryCondition()) ground = BFBlockGeometry( 'ground', (v3, v2, v1, v0), (range(4),), ((v3, v2, v1, v0),), WindTunnelGroundBoundaryCondition(ablConditions)) # return the class wt = cls(name, inlet, outlet, (rightSide, leftSide), top, ground, geometries, roughness, meshingParameters, Zref, convertToMeters) return wt
def x_axis(self, v): """X axis.""" v = v or (1, 0, 0) self._x_axis = vectormath.normalize((v[0], v[1], 0))