Skip to content

Commit

Permalink
Uploading base version
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinodh Rajan committed May 16, 2016
1 parent 8ce44c2 commit 89cb6de
Show file tree
Hide file tree
Showing 99 changed files with 42,827 additions and 0 deletions.
17 changes: 17 additions & 0 deletions CurveReduction/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>CurveReduction</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.python.pydev.PyDevBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.python.pydev.pythonNature</nature>
</natures>
</projectDescription>
8 changes: 8 additions & 0 deletions CurveReduction/.pydevproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse-pydev version="1.0"?><pydev_project>
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
<path>/CurveReduction</path>
</pydev_pathproperty>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
</pydev_project>
Empty file added CurveReduction/__init__.py
Empty file.
177 changes: 177 additions & 0 deletions CurveReduction/curvereduction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import time

from PySide import QtCore, QtGui,QtUiTools

from scipy.interpolate import splev,splprep
from douglas import reduceP

import sys
import matplotlib
matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'
import pylab

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure

class ScribbleArea(QtGui.QWidget):
def __init__(self, parent=None):
super(ScribbleArea, self).__init__(parent)

self.setAttribute(QtCore.Qt.WA_StaticContents)
self.modified = False
self.scribbling = False
self.myPenWidth = 1
self.myPenColor = QtCore.Qt.blue
self.image = QtGui.QImage()
self.lastPoint = QtCore.QPoint()

self.pointList = []
self.pointTime = []
self.start = time.time()

self.pointListR = []

self.addImage()

# newSize = QtCore.QSize(531, 671)
# newImage = QtGui.QImage(newSize, QtGui.QImage.Format_RGB32)
# newImage.fill(QtGui.qRgb(255, 255, 255))
# painter = QtGui.QPainter(newImage)
# painter.drawImage(QtCore.QPoint(0, 0), self.image)
# self.image = newImage

def addImage(self):
newSize = QtCore.QSize(531, 671)
newImage = QtGui.QImage(newSize, QtGui.QImage.Format_RGB32)
newImage.fill(QtGui.qRgb(255, 255, 255))
painter = QtGui.QPainter(newImage)
painter.drawImage(QtCore.QPoint(0, 0), self.image)
self.image = newImage

def clearImage(self):
self.image.fill(QtGui.qRgb(255, 255, 255))
self.modified = True
self.update()

def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
self.lastPoint = event.pos()
self.scribbling = True

def mouseMoveEvent(self, event):
if (event.buttons() & QtCore.Qt.LeftButton) and self.scribbling:
self.drawLineTo(event.pos())

def mouseReleaseEvent(self, event):
if event.button() == QtCore.Qt.LeftButton and self.scribbling:
self.drawLineTo(event.pos())
self.scribbling = False

def tabletEvent(self,event):
if event.pressure() > 0:
print event.pressure(),event.tangentialPressure(),event.xTilt(),event.yTilt(),event.x(),event.y(),event.device() #hiResGlobal
if self.modified == False:
self.pointListR.append((QtCore.QPoint(event.x(),event.y()),time.time()-self.start))
self.lastPoint = QtCore.QPoint(event.x(),event.y())
self.modified = True
else:
self.pointListR.append((QtCore.QPoint(event.x(),event.y()),time.time()-self.start))
self.drawLineTo(QtCore.QPoint(event.x(),event.y()))
elif event.pressure() == 0:
self.modified = False

def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.drawImage(QtCore.QPoint(0, 0), self.image)

def drawLineTo(self, endPoint):
painter = QtGui.QPainter(self.image)
painter.setPen(QtGui.QPen(self.myPenColor, self.myPenWidth,
QtCore.Qt.SolidLine, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin))
if self.lastPoint != endPoint:
painter.drawLine(self.lastPoint, endPoint)
self.modified = True

self.pointList.append(self.lastPoint)
self.pointTime.append(((self.lastPoint,endPoint),time.time()-self.start))

rad = self.myPenWidth / 2 + 2
self.update(QtCore.QRect(self.lastPoint, endPoint).normalized().adjusted(-rad, -rad, +rad, +rad))
self.lastPoint = QtCore.QPoint(endPoint)

class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()

Loader = QtUiTools.QUiLoader()
uiFile = QtCore.QFile("C:/Qt/Qt5.0.1/Tools/QtCreator/bin/ScriptAnalyzerNew/scriptreduce.ui")

uiFile.open(QtCore.QFile.ReadOnly)

self.scribbleWindow = Loader.load(uiFile)

self.scribbleArea = ScribbleArea()
layout = QtGui.QVBoxLayout()
layout.addWidget(self.scribbleArea)
self.scribbleWindow.scribbleArea.setLayout(layout)

self.dpi = 100
self.fig = Figure((6.0, 4.0), dpi=self.dpi)
self.axes = self.fig.add_subplot(111)
self.axes.invert_yaxis()

self.canvas = FigureCanvas(self.fig)
layout = QtGui.QVBoxLayout()
layout.addWidget(self.canvas)
self.canvas.setParent(self.scribbleWindow.velocityArea)

self.scribbleWindow.velocityArea.setLayout(layout)

self.scribbleWindow.velocityBtn.clicked.connect(self.reducePnts)
self.scribbleWindow.clearBtn.clicked.connect(self.clear)

self.scribbleWindow.smoothingTxt.setPlainText('11')


def clear(self):
self.scribbleArea.clearImage()
self.scribbleArea.pointList = []

self.axes.clear()
self.axes.axis('Equal')
self.axes.grid(True)
self.canvas.draw()

def reducePnts(self):
pointList = [(p.x(),p.y()) for p in self.scribbleArea.pointList]

self.axes.clear()
self.axes.axis('Equal')
self.axes.grid(True)

if self.scribbleWindow.smoothingTxt.toPlainText() != "":
errT = int(self.scribbleWindow.smoothingTxt.toPlainText())
else:
errT = 11

smPnts,tck = reduceP(pointList,errT)

xRed = [p[0] for p in smPnts]
yRed = [p[1] for p in smPnts]

uout = list((float(i) / 300 for i in xrange(300 + 1)))
xSp,ySp = splev(uout,tck)

self.scribbleWindow.minPntLbl.setText(str(len(smPnts)))

self.axes.plot(xRed,yRed,'go')
self.axes.plot(xSp,ySp,'r')

self.canvas.draw()

if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.scribbleWindow.show()
sys.exit(app.exec_())
160 changes: 160 additions & 0 deletions CurveReduction/douglas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# pure-Python Douglas-Peucker line simplification/generalization
#
# this code was written by Schuyler Erle <schuyler@nocat.net> and is
# made available in the public domain.
#
# the code was ported from a freely-licensed example at
# http://www.3dsoftware.com/Cartography/Programming/PolyLineReduction/
#
# the original page is no longer available, but is mirrored at
# http://www.mappinghacks.com/code/PolyLineReduction/

"""
>>> line = [(0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,1),(0,0)]
>>> simplify_points(line, 1.0)
[(0, 0), (2, 0), (2, 2), (0, 2), (0, 0)]
>>> line = [(0,0),(0.5,0.5),(1,0),(1.25,-0.25),(1.5,.5)]
>>> simplify_points(line, 0.25)
[(0, 0), (0.5, 0.5), (1.25, -0.25), (1.5, 0.5)]
"""

import math

def simplify_points (pts, tolerance):
anchor = 0
floater = len(pts) - 1
stack = []
keep = set()

stack.append((anchor, floater))
while stack:
anchor, floater = stack.pop()

# initialize line segment
if pts[floater] != pts[anchor]:
anchorX = float(pts[floater][0] - pts[anchor][0])
anchorY = float(pts[floater][1] - pts[anchor][1])
seg_len = math.sqrt(anchorX ** 2 + anchorY ** 2)
# get the unit vector
anchorX /= seg_len
anchorY /= seg_len
else:
anchorX = anchorY = seg_len = 0.0

# inner loop:
max_dist = 0.0
farthest = anchor + 1
for i in range(anchor + 1, floater):
dist_to_seg = 0.0
# compare to anchor
vecX = float(pts[i][0] - pts[anchor][0])
vecY = float(pts[i][1] - pts[anchor][1])
seg_len = math.sqrt( vecX ** 2 + vecY ** 2 )
# dot product:
proj = vecX * anchorX + vecY * anchorY
if proj < 0.0:
dist_to_seg = seg_len
else:
# compare to floater
vecX = float(pts[i][0] - pts[floater][0])
vecY = float(pts[i][1] - pts[floater][1])
seg_len = math.sqrt( vecX ** 2 + vecY ** 2 )
# dot product:
proj = vecX * (-anchorX) + vecY * (-anchorY)
if proj < 0.0:
dist_to_seg = seg_len
else: # calculate perpendicular distance to line (pythagorean theorem):
dist_to_seg = math.sqrt(abs(seg_len ** 2 - proj ** 2))
if max_dist < dist_to_seg:
max_dist = dist_to_seg
farthest = i

if max_dist <= tolerance: # use line segment
keep.add(anchor)
keep.add(floater)
else:
stack.append((anchor, farthest))
stack.append((farthest, floater))

keep = list(keep)
keep.sort()
return [pts[i] for i in keep]

from scipy.interpolate import splev,splprep

def makeSpline(pointList,smPnts):
x = [p[0] for p in pointList]
y = [p[1] for p in pointList]

xRed = [p[0] for p in smPnts]
yRed = [p[1] for p in smPnts]

# print xRed
# print yRed
tck,uout = splprep([xRed,yRed],s=0.,k=2,per=False)
tckOri, uout = splprep([x,y],s=0.,k=2,per=False)

N=300

uout = list((float(i) / N for i in xrange(N + 1)))

xOri, yOri = splev(uout,tckOri)
xSp,ySp = splev(uout,tck)

import dtw
diff = dtw.dynamicTimeWarp(zip(xOri,yOri), zip(xSp,ySp))

err = diff/len(xSp)

return tck,err

def reduceP(pointList,errT=11):
x = [p[0] for p in pointList]
y = [p[1] for p in pointList]

pointList = zip(x,y)
smPnts = zip(x,y)
err = 0
sTxt = 0

count = 0
i=1

#print pointList

oldLength = len(pointList)

while err < errT and len(smPnts) > 3:
smPnts = simplify_points(pointList, sTxt)

if len(smPnts) < 3:
return smPnts,None

tck,err = makeSpline(pointList, smPnts)

if count == 5:
count = 0
i += 2
#print "Incrementing"

sTxt += i

if len(smPnts) == oldLength:
count += 1

oldLength = len(smPnts)

#print err, sTxt, len(smPnts)

if err > errT+2:
#print "Error beyond recognition: reverting to previous"
#print err, sTxt, len(smPnts)
sTxt -= i+i
smPnts = simplify_points(pointList, sTxt)
tck,err = makeSpline(pointList, smPnts)
#print err, sTxt, len(smPnts)

return smPnts,tck
Binary file added CurveReduction/douglas.pyc
Binary file not shown.
Loading

0 comments on commit 89cb6de

Please sign in to comment.