-- ************************************************** -- Provide Moho with the name of this script object -- ************************************************** ScriptName = "MS_PointReduce" -- ************************************************** -- General information about this script -- ************************************************** MS_PointReduce = {} function MS_PointReduce:Name() return "Point Reducer" end function MS_PointReduce:Version() return "1.0" end function MS_PointReduce:Description() return "Naively reduces point density" end function MS_PointReduce:Creator() return "Myles Strous" end function MS_PointReduce:UILabel() return("Point Reducer") end -- ************************************************** -- Recurring values -- ************************************************** MS_PointReduce.minDistance = 0.1 MS_PointReduce.minCurviness = 0.2 MS_PointReduce.retainEverySecond = false -- ************************************************** -- Point reduction dialog -- ************************************************** local MS_PointReduceDialog = {} MS_PointReduceDialog.UPDATE = MOHO.MSG_BASE function MS_PointReduceDialog:new(moho) local d = LM.GUI.SimpleDialog("Point reduction", MS_PointReduceDialog) local l = d:GetLayout() d.moho = moho l:PushH() l:PushV() l:AddChild(LM.GUI.StaticText("Minimum separation"), LM.GUI.ALIGN_LEFT) l:AddChild(LM.GUI.StaticText("Pointiness to retain"), LM.GUI.ALIGN_LEFT) l:Pop() l:PushV() d.minDistance = LM.GUI.TextControl(0, "0.0000", MS_PointReduceDialog.UPDATE, LM.GUI.FIELD_UFLOAT) d.minDistance:SetConstantMessages(true) l:AddChild(d.minDistance) d.minCurviness = LM.GUI.TextControl(0, "0.0000", MS_PointReduceDialog.UPDATE, LM.GUI.FIELD_UFLOAT) d.minCurviness:SetConstantMessages(true) l:AddChild(d.minCurviness) l:Pop() l:Pop() d.retainEverySecond = LM.GUI.CheckBox("Retain every second point") l:AddChild(d.retainEverySecond, LM.GUI.ALIGN_LEFT) return d end function MS_PointReduceDialog:UpdateWidgets() self.minDistance:SetValue(MS_PointReduce.minDistance) self.minCurviness:SetValue(MS_PointReduce.minCurviness) self.retainEverySecond:SetValue(MS_PointReduce.retainEverySecond) end function MS_PointReduceDialog:OnOK() MS_PointReduce.minDistance = self.minDistance:FloatValue() MS_PointReduce.minCurviness = self.minCurviness:FloatValue() MS_PointReduce.retainEverySecond = self.retainEverySecond:Value() end -- ************************************************** -- The guts of this script -- ************************************************** function MS_PointReduce:IsEnabled(moho) if (moho.layer:LayerType() ~= MOHO.LT_VECTOR) then return false end if (moho.layer:CurrentAction() ~= "") then return false -- creating new objects in the middle of an action can lead to unexpected results end return true end function MS_PointReduce:Run(moho) local dlog = MS_PointReduceDialog:new(moho) if (dlog:DoModal() == LM.GUI.MSG_CANCEL) then return end local mesh = moho:Mesh() for c = 0, mesh:CountCurves()-1 do local curve = mesh:Curve(c) local lastPt = nil local deleted = false for i = 0, curve:CountPoints() - 1 do local point = curve:Point(i) curviness = curve:GetCurvature(i, 0) -- if this is the first point we've come across if lastPt == nil then lastPt = point deleted = false -- leave it alone if it connects more than one curve elseif point:CountCurves() > 1 then -- print("Member of more than one curve", point:CountCurves()) lastPt = point deleted = false -- how close is it to the previous point elseif (math.abs(point.fPos.x - lastPt.fPos.x) + math.abs(point.fPos.y - lastPt.fPos.y)) >= self.minDistance then lastPt = point deleted = false -- is this an important shaping point - very small curviness else if curviness < self.minCurviness then lastPt = point deleted = false elseif self.retainEverySecond == true then if deleted == true then lastPt = point deleted = false else -- mesh.DeletePoint(point) curve:Point(i).fSelected = true -- print("This point would have been deleted") deleted = true -- print("curve", c, "point", i, "curviness", curviness) end else -- mesh.DeletePoint(point) curve:Point(i).fSelected = true -- print("This point would have been deleted") deleted = true -- print("curve", c, "point", i, "curviness", curviness) end end end end end