Sunday, July 20, 2008

Codes for Blending Functions of NURBS

Recently I have been interested in computing the value of the blending functions of NURBS. I provided two codes for computing the blending functions here. One uses the recursive method and the other uses the non-recursive method.

Recursive Method
pros : Easier to implement; No need for extra loops
cons : Need extra memory for function calls

Non-recursive Method
pros : Faster than recursive method; No need for ask for extra memory for function calls
cons : Need an array and need to use for-loop

Files : main.m(for testing) , BlendingU.m(recursive) ,
BlendingFun.m(non-recursive)

In the following, I will show how to obtain Np3 (p=1,2,3, ..., 8) and get the plot of the Blending function which will be shown in the end. Note one can change the 3 to 1 and 2 to see the difference. I mean to obtain Np1 and Np2.

main.m
clear
clc

%% Knot vector & Normalization
U=[0 0 0 1 2 3 4 4 5 5 5];
U=U/max(U);

%% Non-recursive
for u=0:0.0001:1
for p=1:1:8
N=BlendingFun(p,3,u,U);
subplot(211),plot(u,N);hold on
end
end
hold off

%% Recursive
for u=0:0.0001:1
for p=1:1:8
N=BlendingU(p,3,u,U);
subplot(212),plot(u,N);hold on
end
end



BlendingU.m
%% Recursive method to compute the blending function
%% Pros : Easier to implement; No need for extra loops
%% Cons : Need extra memory for function calls

function N=BlendingU(n1,pp,uu,Ubar)

if pp==1
if uu>Ubar(1,n1) & uu<Ubar(1,n1+1)
N=1;
else
N=0;
end
else
N1N=BlendingU(n1,pp-1,uu,Ubar);
N2N=BlendingU(n1+1,pp-1,uu,Ubar);
N=0;
if N1N~=0 & Ubar(1,n1+pp-1)-Ubar(1,n1)~=0
N1=(uu-Ubar(1,n1))/(Ubar(1,n1+pp-1)-Ubar(1,n1));
N=N1*N1N;
end
if N2N~=0 & Ubar(1,n1+pp)-Ubar(1,n1+1)~=0
N2=(Ubar(n1+pp)-uu)/(Ubar(1,n1+pp)-Ubar(1,n1+1));
N=N+N2*N2N;
end
end



BlendingFun.m
%% None-recursive method to compute the blending function
%% Only the associated k elements in U are used in this function
%% Need a 1 by (1+k)*k*0.5 array, say ARRAY in this function
%% Pros : Faster than recursive method; No need for ask for extra memory for
%% function calls
%% Cons : Need an array and need to use for-loop

function N=BlendingFun(i,k,u,U)

U=U(i:i+k);
ARRAY=zeros(1,(1+k)*k*0.5);

for ind=0:1:k-1
if u>=U(ind+1) & u<U(ind+1+1)
ARRAY(ind+1)=1;
end
end

layer=2;
pt=k+1;

while layer<=k
for x=0:1:(k+1)-layer-1
if U(x+1+layer-1)-U(x+1)~=0 & ARRAY(pt+x-(k+1-layer)-1)~=0
ARRAY(pt+x)=((u-U(x+1))/(U(x+1+layer-1)-U(x+1)))*ARRAY(pt+x-(k+1-layer)-1);
end
if U(x+1+1+layer-1)-U(x+1+1)~=0 & ARRAY(pt+x-(k+1-layer))~=0
ARRAY(pt+x)=ARRAY(pt+x)+((U(x+1+1+layer-1)-u)/(U(x+1+1+layer-1)-U(x+1+1)))*ARRAY(pt+x-(k+1-layer));
end
if x==(k+1)-layer-1
layer=layer+1;
pt=pt+(k+2)-layer;
end
end
end

%% Return the last element of ARRAY
N=ARRAY((1+k)*k*0.5);



Tuesday, July 15, 2008

ConsoleIn of C

Files: JohnIn.h, JohnIn.c

1. JohnIn.h
---------------------------------------------

/* JohnIn.h
*
* abstract data type
*
* Operator: JohnIn_int , JohnIn_char
*/


/*
* JohnIn_int
*/
extern int int_console(void);

/*
* JohnIn_cdouble
*/
extern double dou_console(void);

/*
* JohnIn_char
*/
extern char char_console(void);



2. JohnIn.c
---------------------------------------------

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include"JohnIn.h"

int int_console(void){
char temp[10];
int value1, test1;

scanf("%s", temp);
value1=atoi(temp);
sscanf(temp, "%d", &test1);
printf("%s", temp);

while(value1!=test1){
printf("\nInput is in wrong format.\n");
printf("Input number with at most 10 digits.\n");
printf("Minus sign is okay.\n");
printf("reenter an integer ---> ");
scanf("%s", temp);
value1=atoi(temp);
sscanf(temp, "%d", &test1);
}
return(value1);
}

double dou_console(void){
char temp[15];
double value2, test2;

scanf("%s", temp);
value2=atof(temp);
sscanf(temp, "%lf", &test2);

while(value2!=test2){
printf("\nInput is in wrong format.\n");
printf("Input number with at most 15 digits.\n");
printf("Minus sign is okay.\n");
printf("reenter an double ---> ");
scanf("%s", temp);
value2=atoi(temp);
sscanf(temp, "%lf", &test2);
}
return(value2);
}

char char_console(void){
char temp[2];

scanf("%s", temp);

while(strlen(temp)!=1){
printf("\nInput is in wrong format.\n");
printf("Input only one char.\n");
printf("reenter a char ---> ");
scanf("%s", temp);
}
return(temp[0]);
}

Sunday, July 13, 2008

Create Album with PHP and Database (3)

Files : Table for uploading, adminup.php

1. Upload images to the server



<form method="post" enctype="multipart/form-data" action="adminup.php">
<table width="480" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width=150 class=side>Name</td>
<td width="330" class=sidetitle>
<?
if($_GET['user']){
echo $_GET['user'];
echo '<input name="name" type="hidden" value="'.$_GET['user'].'">';
}
?>
</td>
</tr>
<tr>
<td width=150 class=side>Album title</td>
<td width="330" class=sidetitle>
<?
if($_GET['albumname']){
echo $_GET['albumname'];
echo '<input name="album" type="hidden" value="'.$_GET['albumname'].'">';
}
?>
</td>
</tr>
<tr>
<td width="480" colspan=2>
<?
if($_GET['id']){
echo '<input name="index" type="hidden" value="'.$_GET['id'].'">';
}
?>
</td>
</tr>
<tr>
<td width=150 class=side>Picture 1</td>
<td width="330" class=sidetitle>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile1" type="file" id="userfile1">
</td>
</tr>
<tr>
<td width=150 class=side>Picture 2</td>
<td width="330" class=sidetitle>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile2" type="file" id="userfile2">
</td>
</tr>
<tr>
<td width=150 class=side>Picture 3</td>
<td width="330" class=sidetitle>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile3" type="file" id="userfile3">
</td>
</tr>
<tr>
<td width=150 class=side>Picture 4</td>
<td width="330" class=sidetitle>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile4" type="file" id="userfile4">
</td>
</tr>
<tr>
<td width=150 class=side>Picture 5</td>
<td width="330" class=sidetitle>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile5" type="file" id="userfile5">
</td>
</tr>
<tr>
<td width=150 class=side>Picture 6</td>
<td width="330" class=sidetitle>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile6" type="file" id="userfile1">
</td>
</tr>
<tr>
<td width=150 class=side>Picture 7</td>
<td width="330" class=sidetitle>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile7" type="file" id="userfile2">
</td>
</tr>
<tr>
<td width=150 class=side>Picture 8</td>
<td width="330" class=sidetitle>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile8" type="file" id="userfile3">
</td>
</tr>
<tr>
<td width=150 class=side>Picture 9</td>
<td width="330" class=sidetitle>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile9" type="file" id="userfile4">
</td>
</tr>
<tr>
<td width=150 class=side>Picture 10</td>
<td width="330" class=sidetitle>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile10" type="file" id="userfile5">
</td>
</tr>
<tr>
<td width="480" class=sidetitle colspan=2><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>


2.when the users click the submit button, it will call adminup.php which is shown below.
adminup.php
<?php

$album=$_POST['album'];
$name=$_POST['name'];
$album_num=$_POST['index'];

$db_conn=mysql_connect("your server", "your id", "your pwd");
mysql_select_db("your database");


//看看這本相簿有多少張相片
$query = "select * from upload where album_num=$album_num";
$result=mysql_query($query);
$how_many_pic=mysql_num_rows($result);

for($i=1;$i<=10;$i++){
$temp="userfile".$i;

if(isset($_POST['upload']) && $_FILES[$temp]['size'] > 0)
{
$fileName = $_FILES[$temp]['name'];
$tmpName = $_FILES[$temp]['tmp_name'];
$fileSize = $_FILES[$temp]['size'];
$fileType = $_FILES[$temp]['type'];

$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

$margin = getimagesize($tmpName);
$width=$margin[0];
$height=$margin[1];

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}

$how_many_pic++;

//Insert the picture into the database. And the table
//to store the pictures is called 'upload'
$query = "INSERT INTO upload (name, size, type, content, album_name, album_num, pic_num, author, width, height ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$album', '$album_num', '$how_many_pic', '$name' , '$width', '$height')";

mysql_query($query) or die('Error, query failed');
}
}

echo '<meta http-equiv=refresh content=0;url="adminpic.php?id='.$album_num.'&user='.$name.'&page=1">';
exit;
?>


How to show the pictures we upload will be introduced next time.