Calculate and return centre of mass of a segment.

    C, seg_volalculated as centre of mass of conical frustum
427935
def segment_centre_of_mass(seg):
    ''''''
    h = mm.segment_length(seg)
    r0 = seg[0][COLS.R]
    r1 = seg[1][COLS.R]
    num = r0 * r0 + 2 * r0 * r1 + 3 * r1 * r1
    denom = 4 * (r0 * r0 + r0 * r1 + r1 * r1)
    centre_of_mass_z_loc = num / denom
    return seg[0][COLS.XYZ] + (centre_of_mass_z_loc / h) * (seg[1][COLS.XYZ] - seg[0][COLS.XYZ])
206979
Point determineClosestCentre(Point p, Point centreA, Point centreB){
		
		//loop counter variable
		int l;	

		//stores the distance between p and centreA
		double distanceA = 0.0;

		for(l=0;l<p.dimension;l++){
			//centroid coordinate of the point
			double centroidCoordinatePoint;
			if(p.weight != 0.0){
				centroidCoordinatePoint = p.coordinates[l] / p.weight;
			} else {
				centroidCoordinatePoint = p.coordinates[l];
			}
			//centroid coordinate of the centre
			double centroidCoordinateCentre;
			if(centreA.weight != 0.0){
				centroidCoordinateCentre = centreA.coordinates[l] / centreA.weight;
			} else {
				centroidCoordinateCentre = centreA.coordinates[l];
			}

			distanceA += (centroidCoordinatePoint-centroidCoordinateCentre) * 
					(centroidCoordinatePoint-centroidCoordinateCentre) ;
		}

		//stores the distance between p and centreB
		double distanceB = 0.0;

		for(l=0;l<p.dimension;l++){
			//centroid coordinate of the point
			double centroidCoordinatePoint;
			if(p.weight != 0.0){
				centroidCoordinatePoint = p.coordinates[l] / p.weight;
			} else {
				centroidCoordinatePoint = p.coordinates[l];
			}
			//centroid coordinate of the centre
			double centroidCoordinateCentre;
			if(centreB.weight != 0.0){
				centroidCoordinateCentre = centreB.coordinates[l] / centreB.weight;
			} else {
				centroidCoordinateCentre = centreB.coordinates[l];
			}

			distanceB += (centroidCoordinatePoint-centroidCoordinateCentre) * 
					(centroidCoordinatePoint-centroidCoordinateCentre) ;
		}

		//return the nearest centre
		if(distanceA < distanceB){
			return centreA;
		} else {
			return centreB;
		}
	}
918427
def centroid(self):
        
        left, bottom, right, top = self.lbrt()
        return (right + left) / 2.0, (top + bottom) / 2.0
984928
def point_in_segment(ac, b):
    '''
    
    '''
    (a,c) = ac
    abc = [np.asarray(u) for u in (a,b,c)]
    if any(len(u.shape) > 1 for u in abc): (a,b,c) = [np.reshape(u,(len(u),-1)) for u in abc]
    else:                                  (a,b,c) = abc
    vab = b - a
    vbc = c - b
    vac = c - a
    dab = np.sqrt(np.sum(vab**2, axis=0))
    dbc = np.sqrt(np.sum(vbc**2, axis=0))
    dac = np.sqrt(np.sum(vac**2, axis=0))
    return np.isclose(dab + dbc, dac) & ~np.isclose(dac,dab) & ~np.isclose(dac,dbc)
1131746
def center(self):
        '''
        
        '''
        return Point(self.midX, self.midY, self.origin.z)
620613
def _vertical_segment(old_offs, new_offs, spacing, radii):
    '''
    '''
    return np.array(((new_offs[0] - radii[0], old_offs[1] + spacing[1]),
                     (new_offs[0] - radii[1], new_offs[1]),
                     (new_offs[0] + radii[1], new_offs[1]),
                     (new_offs[0] + radii[0], old_offs[1] + spacing[1])))
1050749
def curvature(self, T):
        """"""
        seg_idx, t = self.T2t(T)
        seg = self[seg_idx]
        if np.isclose(t, 0) and (seg_idx != 0 or self.end==self.start):
            previous_seg_in_path = self._segments[
                (seg_idx - 1) % len(self._segments)]
            if not seg.joins_smoothly_with(previous_seg_in_path):
                return float('inf')
        elif np.isclose(t, 1) and (seg_idx != len(self) - 1 or
                                   self.end == self.start):
            next_seg_in_path = self._segments[
                (seg_idx + 1) % len(self._segments)]
            if not next_seg_in_path.joins_smoothly_with(seg):
                return float('inf')
        dz = self.derivative(T)
        ddz = self.derivative(T, n=2)
        dx, dy = dz.real, dz.imag
        ddx, ddy = ddz.real, ddz.imag
        return abs(dx*ddy - dy*ddx)/(dx*dx + dy*dy)**1.5
1148635
def segment_tripoints(self, ratio=0.33333):
        

        segments = self.identify_segments()
        points = self.points

        mids1 = ratio*points[segments[:,0]] + (1.0-ratio)*points[segments[:,1]]
        mids2 = (1.0-ratio)*points[segments[:,0]] + ratio*points[segments[:,1]]

        mids = np.vstack((mids1,mids2))
        mid_xpt, mid_ypt = mids[:,0], mids[:,1]

        return mid_xpt, mid_ypt
292482
def get_edot(F, mc, e):
    

    # chirp mass
    mc *= SOLAR2S

    dedt = -304/(15*mc) * (2*np.pi*mc*F)**(8/3) * e * \
        (1 + 121/304*e**2) / ((1-e**2)**(5/2))

    return dedt
673323
private double computeSegmentationScore(Segmentation segmentation) {
		double sum = 0;
		int index = 0;
		for(Segment s:segmentation.getSegments()) {
			SegmentScoreEntry scoreEntry = index == (segmentation.size() - 1) ? 
					getBestInflectedScoreEntry(s, this.inflectionRules):
						getBestInflectedScoreEntry(s, this.transformationRules);
			sum+=scoreEntry.getScore();
			s.setLemma(scoreEntry.getDicoEntry() == null ? 
					s.getSubstring() : 
						scoreEntry.getDicoEntry().getText());
			index++;
		}
		return sum / segmentation.size();
	}
952471
def _horizontal_segment(old_offs, new_offs, spacing, diameter):
    '''
    '''
    return np.array(((old_offs[0], old_offs[1] + spacing[1]),
                     (new_offs[0], old_offs[1] + spacing[1]),
                     (new_offs[0], old_offs[1] + spacing[1] - diameter),
                     (old_offs[0], old_offs[1] + spacing[1] - diameter)))
351410
def frustum(left, right, bottom, top, znear, zfar):
  """"""
  assert right != left
  assert bottom != top
  assert znear != zfar

  M = np.zeros((4, 4), dtype=np.float32)
  M[0, 0] = +2.0 * znear / (right - left)
  M[2, 0] = (right + left) / (right - left)
  M[1, 1] = +2.0 * znear / (top - bottom)
  M[3, 1] = (top + bottom) / (top - bottom)
  M[2, 2] = -(zfar + znear) / (zfar - znear)
  M[3, 2] = -2.0 * znear * zfar / (zfar - znear)
  M[2, 3] = -1.0
  return M
832642
def compute_center_of_mass(self, filename):
        
        sens = np.loadtxt(filename, skiprows=1)

        X = sens[:, 0]
        Z = sens[:, 1]
        # C = (np.abs(sens[:,2]))# ./ np.max(np.abs(sens[:,2]))
        C = sens[:, 2]

        x_center = 0
        z_center = 0
        sens_sum = 0

        for i in range(0, C.shape[0]):
            # unweighted
            if(self.weight == 0):
                weight = (C[i])
            # abs
            if(self.weight == 1):
                weight = np.abs(C[i])
            # log10
            if(self.weight == 2):
                weight = np.log10(np.abs(C[i]))
            # sqrt
            if(self.weight == 3):
                weight = np.sqrt(np.abs(C[i]))

            x_center += (X[i] * weight)
            z_center += (Z[i] * weight)
            sens_sum += weight
        x_center /= sens_sum
        z_center /= sens_sum

        return (x_center, z_center)
753840
def radius_of_gyration(neurite):
    ''''''
    centre_mass = neurite_centre_of_mass(neurite)
    sum_sqr_distance = 0
    N = 0
    dist_sqr = [distance_sqr(centre_mass, s) for s in nm.iter_segments(neurite)]
    sum_sqr_distance = np.sum(dist_sqr)
    N = len(dist_sqr)
    return np.sqrt(sum_sqr_distance / N)
636086
def segment(self):
        
        # half the height
        half = self.primitive.height / 2.0
        # apply the transform to the Z- aligned segment
        points = np.dot(
            self.primitive.transform,
            np.transpose([[0, 0, -half, 1], [0, 0, half, 1]])).T[:, :3]
        return points
943480
def center_of_mass(self):
        
        center = np.zeros(3)
        total_weight = 0
        for site in self:
            wt = site.species.weight
            center += site.coords * wt
            total_weight += wt
        return center / total_weight
586688
def LaplaceCentreWeight(self):
        """"""

        sz = [1,] * self.S.ndim
        for ax in self.axes:
            sz[ax] = self.S.shape[ax]
        lcw = 2*len(self.axes)*np.ones(sz, dtype=self.dtype)
        for ax in self.axes:
            lcw[(slice(None),)*ax + ([0, -1],)] -= 1.0
        return lcw