/*
-----------------------------------------------------------------------------
This source file is part of OpenSpace3D
For the latest info, see http://www.openspace3d.com
Copyright (c) 2012 I-maginer
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
http://www.gnu.org/copyleft/lesser.txt
-----------------------------------------------------------------------------
*/
/*
Version : 1.0
First version : 05/08/2010
Author : Bourineau Bastien
*/
struct ObjBlobDetectorStr = [
PBD_inst : PInstance,
PBD_detector : ObjBlobDetector,
PBD_tLastBlobs : [[I I F] r1],
PBD_iMindist : I,
PBD_iMinrepeat : I,
PBD_Threshold : [I I I],
PBD_Area : [I I I],
PBD_circ : [I F F],
PBD_color : [I I],
PBD_convex : [I F F],
PBD_inertia : [I F F]
]mkObjBlobDetectorStr;;
/*! \brief Callback on instance destruction
*
* Prototype: fun [PInstance ObjJoypad] I
*
* \param PInstance : destroyed plugIT instance
* \param ObjJoypad : joypad object
*
* \return I : 0
**/
fun deleteOb(inst, obstr)=
setPluginInstanceCbPreRender inst nil;
_DSblobDetector obstr.PBD_detector;
0;;
fun getClosestPoint(offset, l, pt)=
let 0 -> found in
(
let offset / 2 -> offset in
let pt -> [px py _] in
let sizelist l -> size in
let 0 -> i in
while ((i < size) && !found) do
(
let nth_list l i -> [x y size] in
if ((px >= (x - offset)) && (px <= (x + offset)) && (py >= (y - offset)) && (py <= (y + offset))) then
set found = 1
else nil;
set i = i + 1;
);
found;
);;
fun cbPreRender(inst, viewstr, obstr)=
let _GETdetectedBlobs obstr.PBD_detector -> nl in
let V3DgetSessionView c3dXsession -> viewstr in
let V3DgetDefaultViewport viewstr -> viewportstr in
let V3DgetViewportSize viewstr viewportstr -> [_ _ vw vh] in
let _GETblobDetectorSize obstr.PBD_detector -> [pw ph] in
let [(itof vw) /. (itof (max pw 1)) (itof vh) /. (itof (max ph 1))] -> [cw ch] in
(
let nl -> l in
while (l != nil) do
(
//scale with surface size
let hd l -> pt in
let pt -> [x y size] in
let ftoi ((itof x) *. cw) -> x in
let ftoi ((itof y) *. ch) -> y in
if (getClosestPoint obstr.PBD_iMindist obstr.PBD_tLastBlobs pt) then
(
SendPluginEvent inst "Update" (strcatnSep (itoa x)::(itoa y)::(ftoa size)::nil " ") nil;
)
else
(
SendPluginEvent inst "Found" (strcatnSep (itoa x)::(itoa y)::(ftoa size)::nil " ") nil;
);
set l = tl l;
);
let obstr.PBD_tLastBlobs -> l in
while (l != nil) do
(
let hd l -> pt in
let pt -> [x y size] in
let ftoi ((itof x) *. cw) -> x in
let ftoi ((itof y) *. ch) -> y in
if (getClosestPoint obstr.PBD_iMindist nl pt) then nil else
(
SendPluginEvent inst "Lost" (strcatnSep (itoa x)::(itoa y)::(ftoa size)::nil " ") nil;
);
set l = tl l;
);
set obstr.PBD_tLastBlobs = nl;
);
0;;
fun cbEnable(inst, from, action, param, rep, obstr)=
if (obstr.PBD_detector != nil) then nil else
(
setPluginInstanceCbPreRender inst mkfun3 @cbPreRender obstr;
set obstr.PBD_detector = _CRblobDetector _channel obstr.PBD_iMindist obstr.PBD_iMinrepeat obstr.PBD_Threshold obstr.PBD_Area obstr.PBD_circ obstr.PBD_color obstr.PBD_convex obstr.PBD_inertia;
);
0;;
fun cbDisable(inst, from, action, param, rep, obstr)=
if (obstr.PBD_detector == nil) then nil else
(
setPluginInstanceCbPreRender inst nil;
_DSblobDetector obstr.PBD_detector;
set obstr.PBD_detector = nil;
);
0;;
/*! \brief Callback on new plugIT instance
*
* Read the parameters from editor values and connect the joypad
*
* Prototype: fun [PInstance] I
*
* \param PInstance : plugIT instance
*
* \return I : 0
**/
fun newOb(inst)=
let atoi (getPluginInstanceParam inst "mindist") -> mindist in
let if mindist == nil then 10 else mindist -> mindist in
let atoi (getPluginInstanceParam inst "minrepeat") -> minrepeat in
let if minrepeat == nil then 1 else minrepeat -> minrepeat in
let atoi (getPluginInstanceParam inst "threshstep") -> threshstep in
let if threshstep == nil then 10 else threshstep -> threshstep in
let atoi (getPluginInstanceParam inst "minthresh") -> minthresh in
let if minthresh == nil then 50 else minthresh -> minthresh in
let atoi (getPluginInstanceParam inst "maxthresh") -> maxthresh in
let if maxthresh == nil then 220 else maxthresh -> maxthresh in
let atoi (getPluginInstanceParam inst "filterarea") -> filterarea in
let if filterarea == nil then 0 else filterarea -> filterarea in
let atoi (getPluginInstanceParam inst "minarea") -> minarea in
let if minarea == nil then 25 else minarea -> minarea in
let atoi (getPluginInstanceParam inst "maxarea") -> maxarea in
let if maxarea == nil then 5000 else maxarea -> maxarea in
let atoi (getPluginInstanceParam inst "filtercirc") -> filtercirc in
let if filtercirc == nil then 1 else filtercirc -> filtercirc in
let atof (getPluginInstanceParam inst "mincirc") -> mincirc in
let if mincirc == nil then 0.8 else mincirc -> mincirc in
let atof (getPluginInstanceParam inst "maxcirc") -> maxcirc in
let if maxcirc == nil then 1.0 else maxcirc -> maxcirc in
let atoi (getPluginInstanceParam inst "filtercolor") -> filtercolor in
let if filtercolor == nil then 0 else filtercolor -> filtercolor in
let atoi (getPluginInstanceParam inst "colorvalue") -> colorvalue in
let if colorvalue == nil then 0 else colorvalue -> colorvalue in
let atoi (getPluginInstanceParam inst "filterconvex") -> filterconvex in
let if filterconvex == nil then 1 else filterconvex -> filterconvex in
let atof (getPluginInstanceParam inst "minconvex") -> minconvex in
let if minconvex == nil then 0.95 else minconvex -> minconvex in
let atof (getPluginInstanceParam inst "maxconvex") -> maxconvex in
let if maxconvex == nil then 1.0 else maxconvex -> maxconvex in
let atoi (getPluginInstanceParam inst "filterinertia") -> filterinertia in
let if filterinertia == nil then 1 else filterinertia -> filterinertia in
let atof (getPluginInstanceParam inst "mininertia") -> mininertia in
let if mininertia == nil then 0.01 else mininertia -> mininertia in
let atof (getPluginInstanceParam inst "maxinertia") -> maxinertia in
let if maxinertia == nil then 1.0 else maxinertia -> maxinertia in
let atoi (getPluginInstanceParam inst "init") -> init in
let if (init == nil) then 1 else init -> init in
let mkObjBlobDetectorStr [inst nil nil mindist minrepeat [threshstep minthresh maxthresh] [filterarea minarea maxarea] [filtercirc mincirc maxcirc] [filtercolor colorvalue] [filterconvex minconvex maxconvex] [filterinertia mininertia maxinertia]] -> obstr in
//let _CRblobDetector _channel 100 16 [5 245 256] [1 1 50] [1 0.2 nil] [1 255] [1 0.82 1.0] [1 0.02 1.0] -> detector in
(
if (!init) then nil else
cbEnable inst nil nil nil nil obstr;
PluginRegisterAction inst "Enable" mkfun6 @cbEnable obstr;
PluginRegisterAction inst "Disable" mkfun6 @cbDisable obstr;
setPluginInstanceCbDel inst mkfun2 @deleteOb obstr;
);
0;;
/*! \brief Global plugIT function to initialize the plugIT callbacks
*
* Prototype: fun [S] I
*
* \param S : plugIT file path
*
* \return I : 0
**/
fun IniPlug(file)=
PlugRegister @newOb nil;
setPluginEditor @dynamicedit;
0;;